SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Thread: Array Questions

  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Questions

    Hi all,

    I'm having some fun with arrays. I'm trying to map printer toners out to an array and I must be doing something wrong.

    PHP:
    PHP Code:
    <?php

    $toners 
    = array();
    $toners[]["Number"] = "M1960G";
    $toners[]["Manufacturer"] = "Apple";
    $toners[]["Printer"][] = "LaserWriter Select 300";
    $toners[]["Printer"][] = "LaserWriter Select 310";
    $toners[]["Printer"][] = "LaserWriter Select 360";
    $toners[]["Color"] = "Black";
    $toners[]["Page Yield"] = "4000";
    $toners[]["MSRP"] = "144.00";
    $toners[]["IEO Price"] = "104.99";


    $toners[]["Number"] = "M1960A";
    $toners[]["Manufacturer"] = "Apple";
    $toners[]["Printer"][] = "LaserWriter Select 300";
    $toners[]["Printer"][] = "LaserWriter Select 310";
    $toners[]["Printer"][] = "LaserWriter Select 360";
    $toners[]["Color"] = "Black";
    $toners[]["Page Yield"] = "4000";
    $toners[]["MSRP"] = "144.00";
    $toners[]["IEO Price"] = "104.99";

    $numberToners count($toners);
    for(
    $i=0;$i<$numberToners;$i++)
    {
        
    $toner "<p>";
        
    $toner .= $toners[$i]["Number"]."<br />";
        
    $toner .= $toners[$i]["Manufacturer"]."<br />";
        
        foreach(
    $toners[$i]["Printer"] as $printer)
        {
            
    $toner .= $printer;
        }
        
        
    $toner .= $toners[$i]["Color"]."<br />";
        
    $toner .= $toners[$i]["Page Yield"]."<br />";
        
    $toner .= $toners[$i]["MSRP"]."<br />";
        
    $toner .= $toners[$i]["IEO Price"];
        
    $toner .= "</p>";
        
            echo 
    $toner;
    }
    ?>
    If you echo that out, you can see the warning for the invalid argument for the foreach. Also, what's with the major spacing between lines?

    Thanks for any help in clearing up some of the mud!
    No, I REALLY dislike having to use Joomla.

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just an fyi, this:

    PHP Code:
    $toners[]["Number"] = "M1960G"
    $toners[]["Manufacturer"] = "Apple"
    means this:

    PHP Code:
    $toners[0]["Number"] = "M1960G"
    $toners[1]["Manufacturer"] = "Apple"
    and not this:

    PHP Code:
    $toners[0]["Number"] = "M1960G"
    $toners[0]["Manufacturer"] = "Apple"
    You could declare out the associative array variables, then use array_push to populate $toners with yourvalue. Same with the ["Printer"] array.

  3. #3
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yeah, I realized that after a var_dump. Thanks for the reply.

    I'm not following what you're saying with array_push though.

    How would I keep manufacturer, all printer models, color, yield, MSRP and IEO Price associated with one OEM?

    This is what I'd like it to look like

    PHP Code:
    array(2){
    [
    0] = > array(7){
        [
    "Number"]=>  string(6"M1960G"
        
    ["Manufacturer"]=>  string(5"Apple"
        
    ["Printer"]=>  array(3){
            [
    0]=> string(22"LaserWriter Select 300"
            
    [1]=> string(22"LaserWriter Select 310"
            
    [2]=> string(22"LaserWriter Select 350"
            
    }
        [
    "Color"]=>  string(5"Black"
        
    ["Page Yield"]=>  string(4"4000"
        
    ["MSRP"]=>  string(4"4000"
        
    ["IEO Price"]=>  string(4"4000"
        
    }

    [
    1] = > array(7){
        [
    "Number"]=>  string(6"M1960A"
        
    ["Manufacturer"]=>  string(5"Apple"
        
    ["Printer"]=>  array(3){
            [
    0]=> string(22"LaserWriter Select 300"
            
    [1]=> string(22"LaserWriter Select 310"
            
    [2]=> string(22"LaserWriter Select 350"
            
    }
        [
    "Color"]=>  string(5"Black"
        
    ["Page Yield"]=>  string(4"4000"
        
    ["MSRP"]=>  string(4"4000"
        
    ["IEO Price"]=>  string(4"4000"
        
    }

    No, I REALLY dislike having to use Joomla.

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Or:
    PHP Code:
    $toners = array(); 
    $toners[] = array(
        
    "Number" => "M1960G",
        
    "Manufacturer" => "Apple",
        
    "Printer" => array(
            
    "LaserWriter Select 300",
            
    "LaserWriter Select 310",
            
    "LaserWriter Select 360"
        
    }
        
    "Color" => "Black",
        
    "Page Yield" => "4000",
        
    "MSRP" => "144.00",
        
    "IEO Price" => "104.99"

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This?

    PHP Code:
    $toners = array();

    $toners[] = array(
    "Number" => "M1960A",
    "Manufacturer" => "Apple",
    "Printer" => array("LaserWriter Select 300""LaserWriter Select 310""LaserWriter Select 360"),
    "Color" => "Black",
    "Page Yield" => "4000",
    "MSRP" => "144.00",
    "IEO Price" => "104.99"
    ); 
    EDIT: whoops, didn't see you there Ark.

    Now I just have to figure out how to iterate through and populate this dynamically. I shall return!
    No, I REALLY dislike having to use Joomla.

  6. #6
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your array is way messed up. Why not try an associative array instead of a numeric one? It perfectly suits your needs here. I simplified your array using a better way of writing arrays as follows (removed the duplicated elements at the end of the array too):

    Code php:
    $toners_modified = array(
                    'Number'            => 'M1960G',
                    'Manufacturer'      => 'Apple',
                    'Printer'           => array(
                                        'LaserWriter Select 300',
                                        'LaserWriter Select 310',
                                        'LaserWriter Select 360'
                                        ),
                    'Color'                => 'Black',
                    'Page Yield'        => '4000',
                    'MSRP'              => '144.00',
                    'IEO Price'         => '1.4.99',
                    'Number'            => 'M1960A'
                    );
    It's good practice to always check the array structure by using print_r() that "Prints human-readable information about a variable" according to the manual.

    PHP Code:
    print_r($toners); 
    About the foreach error, it's because you're passing array elements as the first argument, whereas it takes only whole arrays not elements of arrays.

    Your ambiguous code makes it hard for you (and us) to debug it.
    Imagination is more important than knowledge. - Einstein

  7. #7
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, just out of curiousity, why the usage of arrays? Just sort of learning about them or any other need?

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The way I would go about it would be having an array of objects, each containing a printer, it's data and an array of ink cartrige objects.

    However, that's because I've been getting so used to Java recently that everything I do is OOP (I'm even using a static 'Main' object to output my latest project - the whole site is OOP from top to bottom, apart from index.php which instantiates the Main class and echos it).

    It'd probably be better to use a db to store this info, and outputting to this kind of array would be simple from parent (printer)/child(cartridge) queries.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_fuel View Post
    Hmm, just out of curiousity, why the usage of arrays? Just sort of learning about them or any other need?
    I have a spreadsheet of inkjets and toners I need to import in to a DB and I'm trying to come up with a solid way of sorting it before inserting.

    If there was only one printer per OEM (which is the identifier for a cart or toner), I wouldn't have a problem.

    It needs to go into a products table. The OEM and Prices are in their own field, but I'm taking the Color, Page Yield and Printer Models and putting them in the description.

    Then, it needs to take each Manufacturer and put them in a subcategories table. Also needs to take each Printer to go into a printers table.

    Then I'm trying to set up a lookup table that associates a printer to a subcategory.

    I'm trying to do this all in one swoop on both my inkjet carts and toners, but there are different columns in the cart spreadsheet from the toners.

    whew...yeah!

    Other than that, I'm trying to learn more about them and I thought this would be a decent exercise.
    No, I REALLY dislike having to use Joomla.

  10. #10
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I'm a bit stumped on some logic.

    How do I add more printers for an OEM that already has an array?

    PHP Code:
    $handle fopen($file"r");//set handle to read-only csv file
    $toners = array();
    $printers = array();
    $OEMlist = array();

    while((
    $data fgetcsv($handle)) !== FALSE)
    {
        list(
    $oem$manufacturer$printermodel$color$yield$msrp$ieoprice) = $data;
        
        if(
    in_array($oem$OEMlist))
        {    
            
    array_push($printers[$oem], $printermodel);
        }else
            {
                
    $printers[$oem] = array($printermodel);
            }
        
        if(!
    in_array($oem$OEMlist))
        {
        
    $toners[] = array(
            
    "Number" => $oem,
            
    "Manufacturer" => $manufacturer,
            
    "Printer" => $printers[$oem],
            
    "Color" => $color,
            
    "Page Yield" => $yield,
            
    "MSRP" => $msrp,
            
    "IEO Price" => $ieoprice);
        
        }
        
    $OEMlist[] = $oem;
        
    }
    print_r($toners); 
    I can see why the printers array is never fully populated. It's getting the loop to maintain the toner array to add the additional printers to correct OEM that is giving me a hard time.

    When I check for the $oem in the $OEMlist, I add a printer model to the already established $printers[$oem]. How would I put the $toners array in that same argument so that the $printers array is updated within the $toners array?

    sheesh...I hope that made sense.
    No, I REALLY dislike having to use Joomla.

  11. #11
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    by the way

    PHP Code:
    print_r($printers); 
    reveals the printers array is correct.
    No, I REALLY dislike having to use Joomla.

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could store the array items with the OEM as the key.

    Then add the toner to a new item in an array within the printer array with the key of the OEM.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #13
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error messages

    I get these errors for:

    Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\wamp\www\inkexchange\upload_db.php on line 79

    Warning: array_push() [function.array-push]: First argument should be an array in C:\wamp\www\inkexchange\upload_db.php on line 81
    PHP Code:
    if(!in_array($printermodel$manfList[$oem][$manufacturer]))//79
        
    {
            
    array_push($manfList[$oem][$manufacturer], $printermodel);//81
        
    }else
        {
            
    $manfList[$oem][$manufacturer] = array($printermodel);
        } 
    Those two functions work fine with $manfList[$oem] but as soon as I add [$manufacturer], it throws that error.

    Isn't $manfList[$oem][$manufacturer] an array??
    No, I REALLY dislike having to use Joomla.

  14. #14
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We cannot tell for sure unless we know what is in $manufacturer. If it is an array then the whole expression is an array. If it is a value then the whole expression is a value, that it, not an array.
    Imagination is more important than knowledge. - Einstein

  15. #15
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so..

    $manfList[$oem][$manufacturer] = array($printermodel);

    could be a value and not an array?
    No, I REALLY dislike having to use Joomla.

  16. #16
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did I say that? It seems that you're misinterpreting the assignment operator to mean (equals), which is very incorrect and will lead you to the assumption that since array($printermodel) is an array, then the whole expression $manfList[$oem][$manufacturer] = array($printermodel); should be an array.

    Your code above assigns the value of $manfList[$oem][$manufacturer] to array($printermodel). Again, if $manfList[$oem][$manufacturer] is a value then you'd be changing array($printermodel)'s type and value to that of $manfList[$oem][$manufacturer].
    Imagination is more important than knowledge. - Einstein

  17. #17
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, after some more playing I think I'm close...

    So when I echo it out of a foreach loop it looks like:
    C3900A
    Apple
    LaserWriter 350 (BX)
    HP
    4V
    4MV
    4V
    4MV
    Black
    8100
    And it needs to look like:
    C3900A
    Apple
    LaserWriter 350 (BX)
    HP
    4V
    4MV
    Black
    8100
    Code:

    PHP Code:
    <?
    $handle 
    fopen($file"r");//set handle to read-only csv file
    $handle2 fopen($file"r");//set handle to read-only csv file
    $toners = array();
    $OEMList = array();
    $OEMList2 = array();
    $man_array = array();
    while((
    $data fgetcsv($handle)) !== FALSE)
    {
        list(
    $oem$manufacturer$printermodel$color$yield$msrp$ieoprice) = $data;
        
        if(
    in_array($oem$OEMList))//Check OEM array for $oem
        
    {    
            if(!
    in_array($printermodel$man_array[$oem]))
            {
                
    $man_array[$oem][$manufacturer][] = $printermodel;
            }
        }else
    //It's not in the OEM list
            
    {
                
    $printers[$oem] = array($printermodel);
                
    $man_array[$oem][$manufacturer][] = $printermodel;
                if(
    $oem != '')
                {
    $OEMList[] = $oem;}
            }
    }
    fclose($handle);
    while((
    $data fgetcsv($handle2)) !== FALSE)
    {
        list(
    $oem2$manufacturer$printermodel$color$yield$msrp$ieoprice) = $data;
        
        if(!
    in_array($oem2$OEMList2) && $oem2!='')
        {
        
    $toners[] = array(
            
    "Number" => $oem2,
            
    "Color" => $color,
            
    "Page Yield" => $yield,
            
    "MSRP" => $msrp,
            
    "IEO Price" => $ieoprice);
            if(
    $oem2 != ''){$OEMList2[] = $oem2;}
        }
    }
    $numberToners count($toners);
    for(
    $i=0;$i<$numberToners;$i++)
    {
        
    $oem $toners[$i]["Number"];
        
    $manufacturers $man_array[$oem];
        
        
        
    $toner "<h3>".$oem ."</h3>\n\r";
            
    $toner .= "<p>\n\r";
        foreach(
    $manufacturers as $man=>$printer)
        {
            
    $toner .= "<strong>".$man."</strong><br />\n\r";
            foreach(
    $printer as $model)
            {
            
    $toner .= "&nbsp;&nbsp;".$model."<br />\n\r";
            }
        }    
        
    $toner .= $toners[$i]["Color"]."<br />\n\r";
        
    $toner .= $toners[$i]["Page Yield"]."<br />\n\r";
        
    $toner .= $toners[$i]["MSRP"]."<br />\n\r";
        
    $toner .= $toners[$i]["IEO Price"];
        
    $toner .= "</p>\n\r";
        echo 
    $toner;
    }

    ?>
    How can I keep the same printer model from repeating itself within manufacturers? Thanks!
    No, I REALLY dislike having to use Joomla.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •