Results 1 to 5 of 5

Thread: Multidimenstional array traversal

  1. #1

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141

    Multidimenstional array traversal

    I have a multidimensional array called $arr_results as follows:
    Code:
    Array
    (
        [0] => Array
            (
                [T] => Title 0
                [D] => Description 0
                [U] => Real URL 0
                [Y] => Fake URL 0
                [C] => Cost 0
                [I] => Image URL 0
            )
    
        [1] => Array
            (
                [T] => Title 1
                [D] => Description 1
                [U] => Real URL 1
                [Y] => Fake URL 1
                [C] => Cost 1
                [I] => Image URL 1
            )
    
        [2] => Array
            (
                [T] => Title 1
                [D] => Description 1
                [U] => Real URL 1
                [Y] => Fake URL 1
                [C] => Cost 1
                [I] => Image URL 1
            )
    )
    I want to loop through each element in the array, and for each one print out it's values for T, D, U, Y, C, I.
    I'm using the following code to do it:
    PHP Code:
    if(count($arr_results) > 1)
    {
        for(
    $counter=0$counter<count($arr_results); $counter++)
        {
            print(
    $counter 1);
            print(
    $arr_results[$counter]["T"]);
            print(
    $arr_results[$counter]["D"]);
            print(
    $arr_results[$counter]["U"]);
            print(
    $arr_results[$counter]["Y"]);
            print(
    $arr_results[$counter]["C"]);
            print(
    $arr_results[$counter]["I"]);
        }

    This code works fine if the main array index starts at 0 and increments by 1.
    My problem is that often it starts at some other number and then the whole thing falls appart.
    Say for example my array looked like this, how would I do it?
    Code:
    Array
    (
        [6] => Array
            (
                [T] => Title 6
                [D] => Description 6
                [U] => Real URL 6
                [Y] => Fake URL 6
                [C] => Cost 6
                [I] => Image URL 6
            )
    
        [8] => Array
            (
                [T] => Title 8
                [D] => Description 8
                [U] => Real URL 8
                [Y] => Fake URL 8
                [C] => Cost 8
                [I] => Image URL 8
            )
    
        [13] => Array
            (
                [T] => Title 8
                [D] => Description 8
                [U] => Real URL 8
                [Y] => Fake URL 8
                [C] => Cost 8
                [I] => Image URL 8
            )
    )

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Like this:

    Code:
    while (list ($number, $vars) = each ($arr_results)) {
        echo "$i<br>";
        echo $vars['T'] . "<br>";
        echo $vars['D'] . "<br>";
        echo $vars['U'] . "<br>";
        echo $vars['Y'] . "<br>";
        echo $vars['C'] . "<br>";
        echo $vars['I'] . "<br>";
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141
    Merci!

    With the slight modification of $i to $number it works perfectly.

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by mralston
    Merci!

    With the slight modification of $i to $number it works perfectly.
    I'm not good with remembering my variable names, even when they're two lines apart.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141
    Heh heh.

Posting Permissions

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



Click Here to Expand Forum to Full Width