views:

34

answers:

2

i have below array structure

$movieCast=Array
(
    [1280741692] => Array
        (
            [moviename] => twlight                
            [movie_cast] => Array
                (
                    [0] => 0000000083
                    [1] => 0000000084
                )
            [userid] => 62
            [country_id] => 00002              
            [doAction] => ADD
        )
     [1280744592] => Array
        (
            [moviename] => love actually                
            [movie_cast] => Array
                (
                    [0] => 0000000083
                    [1] => 0000000084
                )
            [userid] => 62
            [country_id] => 00002              
            [doAction] => ADD
        )

)

i just need the country_id from above array, whereas each array key can be anything( timestamp) but $movieCast will have same country in all array inside that.,

+3  A: 

You have to iterate through the outer array:

foreach ($outer as $inner) {
    //do something with $inner["country_id"]
}

Another option is to build an array with the contry_ids (example uses PHP >=5.3 functionality, but that can be worked around easily in earlier versions):

array_map(function ($inner) { return $inner["country_id"]; }, $outer);

EDIT If the ids are all the same, even easier. Do:

$inner = reset($outer); //gives first element (and resets array pointer)
$id = $inner["country_id"];
Artefacto
i just need the id not the list of country id bcoz all country id are same?
JustLearn
@Just OK, I edited.
Artefacto
why we use foreach, i don't want to looping. i just want first array country_id, foreach took time if array is too large( actually it is session array)
JustLearn
@Just At the bottom...
Artefacto
@Artefacto is it fine to use below code using break? ` foreach ($movieCast as ` `break;` `}`
JustLearn
@Just It would work, but it's less simple than just `$inner = reset($outer); $id = $inner["country_id"];`. Why would you want to make a loop that does only one iteration. always.
Artefacto
@Artefacto Thankyou for help.. i was using foreach unnecesarily.thanks
JustLearn
A: 

a more general-purpose solution using php 5.3:

function pick($array,$column) {
    return array_map(
        function($record) use($column) {
            return $record[$column];
        },
        $array
    );
}
stillstanding