views:

37

answers:

1

I need to iterate through a dynamic array. The array will look something like the following:

Array
(
    [2010091907] => Array
        (
            [home] => Array
            (
                [score] => Array
                (
                        [1] => 7
                        [2] => 17
                        [3] => 10
                        [4] => 7
                        [5] => 0
                        [T] => 41
                    )

                [abbr] => ATL
                [to] => 2
            )

        [away] => Array
            (
                [score] => Array
                    (
                        [1] => 0
                        [2] => 7
                        [3] => 0
                        [4] => 0
                        [5] => 0
                        [T] => 7
                    )

                [abbr] => ARZ
                [to] => 2
            )

        [weather] => 
        [media] => Array
            (
                [tv] => FOX
                [sat] => 709
                [sathd] => 709
                [radio] => Array
                    (
                        [home] => 153
                        [away] => 90
                    )

            )

        [bp] => 13
        [yl] => 
        [qtr] => Final
        [down] => 0
        [togo] => 0
        [clock] => 00:26
        [posteam] => ARZ
        [note] => 
        [redzone] => 
        [stadium] => Georgia Dome
    )

I need it to be dynamic and for testing purposes, I need to be able to call it via:

echo "Key: $key; Value: $value<br />\n";

I'm going to later take this information and place it into a mysql database, but for now, I need to brush up on arrays and figure out how to format the data.

Any help is appreciated.

+1  A: 

I´d go for a recursive function: If a value is an array, call it again and otherwise display the key / value pair.

Something like (not tested):

function display_array($your_array)
{
    foreach ($your_array as $key => $value)
    {
        if is_array($value)
        {
            display_array($value);
        }
        else
        {
             echo "Key: $key; Value: $value<br />\n";
        }
    }
}

display_array($some_array);
jeroen
This definately worked. I just have to figure out how, using this information, I can call objects out of the array, such as the value of "home", etc.
drewrockshard
I initially thought you might want to get individual values of keys, but I dismissed it as you had a lot of duplicate keys like `home`, `away`, `to`, etc. Glad you got it working!
jeroen