Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through array and get key and value [duplicate]

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.

like image 384
drewrockshard Avatar asked Sep 20 '10 16:09

drewrockshard


People also ask

How to find duplicate value in array?

function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = 0; j < myArray. length; j++) { if (i != j) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } } return false; // means there are no duplicate values. }

Can array have duplicate keys?

Arrays contains unique key. Hence if u are having multiple value for a single key, use a nested / multi-dimensional array. =) thats the best you got.


1 Answers

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);
like image 128
jeroen Avatar answered Oct 13 '22 13:10

jeroen