Array php go through

Asked

Viewed 256 times

0

To the array:

Array
(
    [45] => Array
        (
            [car] => stdClass Object
                (
                    [consortiums] => stdClass Object
                (
        ),
    [92] => Array
        (
            [car] => stdClass Object
                (
                    [consortiums] => stdClass Object
                (
        )
)   

How I can walk all the consortia?

  • I don’t understand the code, is this a result of var_dump() ? your question may be how to take the two array ( with Dice 92 and 25 ) taken by the Dice of the first array would not solve ?

  • and that’s right, and I’m killing myself thinking I’d have to capture the array by those numbers. vlw

1 answer

1

Assuming this structure holds, you can get it with a simple foreach:

<?php

$consortiums = array();
foreach($arr as $arri)
    array_push($consortiums,$arri['car']->consortiums);

print_r($consortiums);

?>

I’m iterating the array $arr (representing your array), ignoring the numerical indexes, and for each one I access the index car, that returns an object that has the member assortment, which in turn returns the final object. I replicated your dump as follows:

<?php

$ob  = new stdClass();
$ob->consortiums = new stdClass();
$ob->consortiums->i_am_a_consortium = "i am!";

$arr = array(
    45 => array("car" => $ob),
    92 => array("car" => $ob)
); 

print_r($arr);

?>

Which produces an output similar to yours (I added only one member to the last object, so that we understand that they are the consortiums):

Array
(
    [45] => Array
        (
            [car] => stdClass Object
                (
                    [consortiums] => stdClass Object
                        (
                            [i_am_a_consortium] => i am!
                        )
                )
        )

    [92] => Array
        (
            [car] => stdClass Object
                (
                    [consortiums] => stdClass Object
                        (
                            [i_am_a_consortium] => i am!
                        )
                )
        )
)

And by testing the cycle, the output is:

Array
(
    [0] => stdClass Object
        (
            [i_am_a_consortium] => i am!
        )

    [1] => stdClass Object
        (
            [i_am_a_consortium] => i am!
        )
)

Which is an array of Consortium.

Your question is somewhat vague, and I can’t quite see exactly how you want the output, but you get an idea.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.