0

I have a multidimensional array that is auto generated from a function. It looks like this:

Array(
[0] => Array
    (
        [0] => stdClass Object
            (
                [team] => Borussia Dortmund (gazeder)
            )

        [1] => stdClass Object
            (
                [team] => Real Madrid (Deycekslo)
            )

    )

[1] => Array
    (
        [0] => stdClass Object
            (
                [team] => Bayern Munchen (DaviiX)
            )

        [1] => stdClass Object
            (
                [team] => AS Roma (jakobmmm)
            )

    )

[2] => Array
    (
        [0] => stdClass Object
            (
                [team] => Chelsea (davorm9)
            )

        [1] => stdClass Object
            (
                [team] => Napoli (pubilegenda)
            )

    )

    ETC...

So I have to extract the "team" values from it and store it into a database. The database part is not a problem, the problem is extracting. I tried several things as this one but it doesn't work:

    $length = count($game->tour);
       for ($row = 0; $row < $length; $row++) {
           for ($col = 0; $col <= 2; $col++) {
           echo "<p>".$game->tour[$row][$col]."</p>";
       }
    }

The only thing that works is that:

    foreach($game->tour[0][0] as $array ) {
        echo $array;
        foreach($game->tour[0][1] as $array2 ) {
            echo $array2;
        }
    }

    foreach($game->tour[1][0] as $array ) {
        echo $array;
        foreach($game->tour[1][1] as $array2 ) {
            echo $array2;
        }
        echo '<br />';
    }

That one works fine but it's garbage because I need to be able to loop through that array and print all the "team" values. Any suggestions?

Thank you!

Cœur
  • 37,241
  • 25
  • 195
  • 267
janip
  • 19
  • 7
  • So you need to extract team values to save it to the database right? not extract to print right? – artsylar Nov 26 '15 at 01:18
  • I believe in the function that is generating that array, is decoding some json? You should add true as a second parameter to json_decode so that it becomes an array rather than an stdClass object. – Daniel Nov 26 '15 at 01:23
  • so i have to write json_decode($game->tour) and then go to loop? – janip Nov 26 '15 at 01:33
  • Function reads the parameters from database (the parameters are teams) and saves them as arrays. – janip Nov 26 '15 at 01:36
  • I added json_dedoce but it gives me empty array. – janip Nov 26 '15 at 01:46
  • So I played a litje with json but that is not an issue. If I write that foreach loop as is writen in the post it gives me the result I need but I have to have the same result for all the other values in array and I need a loop for that. – janip Nov 26 '15 at 02:06
  • seems related: https://stackoverflow.com/questions/1118994/php-extracting-a-property-from-an-array-of-objects – k0pernikus Nov 26 '15 at 02:40

1 Answers1

0

untested, but i guess this should work

function ext($obj,$name){
    $ret=array();
    $rem=array();
    $f=function() use(&$rem,&$ret,$name){
        $v=reset($rem);
        assert(NULL!==($key1=key($rem)));
        unset($rem[$key1]);
        foreach($v as $key2=>$value){
            if($key2==$name){$ret[]=$value;}
            if(is_array($value) || is_object($value))
            {
                $rem[]=$value;
            }
        }
    };
    $rem[]=$obj;
    while(!empty($rem))
    {
        $f();
    }       
    return $ret;
}

$teams=ext($arr,'team');

Edit: fixed a typo in a variable.. Edit2: warning, this might go in an infinite loop if you have any circular references... dunno, you should test if you're worried about that. edit3: fixed another variable name typo x.x (which would break the code)

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • just to clarify: with $arr I call the array? – janip Nov 26 '15 at 02:27
  • the function* yes. you call the ext() function with your array as the first parameter, and the value name you're looking for in the 2nd parameter (team) – hanshenrik Nov 26 '15 at 02:28
  • ok it seems like it works but if I type in echo $teams just to see what comes out it gives me just Array not all the team objects – janip Nov 26 '15 at 02:30
  • It actually works almost fine but yes it's a infinite loop – janip Nov 26 '15 at 02:39