views:

45

answers:

3

I'm so tired of arrays today - thrown me all over the place.

So, here's the output of an array:

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

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

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

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

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

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

I'm going to be writting a preg_match to iterate each [2010091907], but before I can, I do not understand how to get this piece of information, or how to call it. I'd be doing something like:

$json=json_decode($data,true);
foreach ($json['dont-know-what-to-call-it'] as $key => $value) {
            echo "Key: ".$key."; Value: ".$value."<br />";
}

I just don't know how to call each one of those [2010091901] blocks, like what name I'm suppose to call them as. I know how to call the stuff under score, since it's named "score", and the data is under all that. I don't know how to get the key/value of the initial "sections" of the array. In the end, I'm going to want to grab each [2010091901], manipulate/use the data that is inbetween each one of the [2010091901], and then go to the next "record".

+2  A: 

$date_keys = array_keys($json) would give (0 => 2010091907, 1 => 2010091909, ...) . then you could do

foreach ($date_keys as $d) {
   foreach ($json[$d] as $key => $value) {
...

Also, if you do not actually need the indices of the outer array (the date values - 2010091907, etc) than you could just do

foreach ($json as $j) {
   foreach ($j as $key => $value) {
...

ignoring the keys of $json

brian_d
+1  A: 

Can't you just nest foreach()s?

foreach($jsondata as $somedate => $value){
    //do you actually need $somedate?
    foreach($value['home']['score'] as $score){
        echo $score.PHP_EOL;
    }
}
Wrikken
I don't have time to test this but I really like this method as far as simplicity. I would actually make the foreach a function that receives data by rel the function can call itself. It would be able to loop until it ran out of child nodes. Avoid using while() or any variation of it.
Geekster
A: 

You can just do

$json = json_decode($data, true);
foreach($json as $ymd => $data)
{
    // $ymd = [2010091907, 2010091909,… ]
    // $data is the array starting with the key home. so $data['home']['score'][1] = 7 for the first iteration
}

This answer your question? It's not 100% clear what you're asking

notJim