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!

7
  • So you need to extract team values to save it to the database right? not extract to print right? Commented Nov 26, 2015 at 1: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. Commented Nov 26, 2015 at 1:23
  • so i have to write json_decode($game->tour) and then go to loop? Commented Nov 26, 2015 at 1:33
  • Function reads the parameters from database (the parameters are teams) and saves them as arrays. Commented Nov 26, 2015 at 1:36
  • I added json_dedoce but it gives me empty array. Commented Nov 26, 2015 at 1:46

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

just to clarify: with $arr I call the array?
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)
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
It actually works almost fine but yes it's a infinite loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.