Thread: Array question

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7

    Array question


    Trying to get the dimensions of a small, medium and large box from the code below. when it runs I get nothing for small box and an undefined index 3 and undefined index 6 error.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
    <title>Box Array</title>
    <meta http-equiv="Content-Type"
        content="text/html; charset=iso-8859-1"/>
    </head>
    <body>
    <?php
    // declare three indexed arrays
    $small = array(12102.5);
    $medium = array(30204);
    $large = array(604011.5);

    // combine three indexed arrays into a multidimensional array

    $boxes = array($small$medium$large);

    // display size of small box
    $smallbox $boxes[0][1][2];
    echo 
    "Small box dimension: " $smallbox "<br />\r\n";

    // display size of Medium box
    $medbox $boxes[3][4][5];
    echo 
    "Small box dimension: " $medbox "<br />\r\n";

    // display size of large box
    $largebox $boxes[6][7][8];
    echo 
    "Large box dimension: " $largebox "<br />\r\n";


    ?>
    </body>
    </html>
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    The first dimension of $boxes only has three elements: 0, 1 and 2. Each of those also has three elements: 0, 1, 2. So none of your array indexes should be greater than 2.

    Additionally, $boxes only has two dimensions, but you're accessing it as if it had three. For a two dimensional array you can only specify two dimensions: $boxes[$a][$b]

    So your array goes from $boxes[0][0] to $boxes[2][2].

    A good way to debug array issues using var_export or print_r:
    PHP Code:
    print_r($boxes); 
    This will show you the exact structure of the array.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    So how would my echo statement look to multiply the elements for the small box deminsions since I already combined them into a multi dimensional array? Just a bit confused. Thanks
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    if you still have trouble understanding the structure or the indexing logic, just do it step by step. The small box array is the first element of $boxes, so you access it with
    PHP Code:
    $small_box $boxes[0]; 
    And now you have a simple one dimensional array consisting of thee numbers. The first one is $small_box[0], the second is $small_box[1] etc.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    PHP Code:
    echo "Small box dimension: " $boxes[0][0] , 'x' $boxes[0][1] , 'x' $boxes[0][2]; 
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Stockholm, Sweden
    Posts
    16
    Rep Power
    0
    Originally Posted by maineman
    So how would my echo statement look to multiply the elements for the small box deminsions since I already combined them into a multi dimensional array? Just a bit confused. Thanks
    Is the following something you're looking for?

    PHP Code:
    <?php
    $a 
    = array(
       
    '0' => array('12''10''2.5'),
       
    '1' => array('30''20''4'),
       
    '2' => array('60''40''11.5')
    );
    $b $a[0];
    $c $a[1];
    $d $a[2];

    echo 
    'Small box dimensions: '$b[0], ' x '$b[1], ' x '$b[2];
    echo 
    'Medium box dimensions: '$c[0], ' x '$c[1], ' x '$c[2];
    echo 
    'Large box dimensions: '$d[0], ' x '$d[1], ' x '$d[2];
    ?>
    Will give the following output:
    Code:
    Small box dimensions: 12 x 10 x 2.5
    Medium box dimensions: 30 x 20 x 4
    Large box dimensions: 60 x 40 x 11.5

IMN logo majestic logo threadwatch logo seochat tools logo