php - Get specific key values as an array from array of object -


this code:

$a = array(     array("a" => 1, "b" => 2),     array("x" => 2, "a" => 2),     array("d" => 100, "a" => 3, "b" => 2, "c" => 3) ); $myarray = array(); foreach ($a $arr) {     $myarray[] = $arr['a']; } print_r($myarray); 

so, get

array (     [0] => 1     [1] => 2     [2] => 3 ) 

is there other way without for loop? using 1 2 php array functions same response.

the above correct still if there other better way appreciable! because same array $a in code required iterate many times. if have better way can reduce iteration( php still iteration in built-in fns, don't bother it).

yes (since you're on php 5.4, array_column() isn't option),

$result = array_map(function($x) {    return $x['a']; }, $a); 

but note, still use loop internally (i.e. in end always loop)


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -