Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite array without second temp array

Tags:

arrays

php

I have an array like this:

$a = array(
        array(
            'a' => 'x',
            'b' => 'asdasd',
        ),
        array(
            'a' => 'f',
            'b' => '123123qwe',
        ),
);

And I expect an array like this:

$a = array(
        'asdasd',
        '123123qwe',
);

I can transform this by iterating and filling a new array, I wonder if I can do this in 1 line without temporary variables?

Update: Using PHP 5.3 , thanks for the 5.5 suggestions tho!

like image 405
Daniel W. Avatar asked Mar 27 '26 16:03

Daniel W.


1 Answers

If you're using PHP 5.5 you can use array_column():

 $new_array = array_column($a, 'b');

See it in action

like image 141
John Conde Avatar answered Mar 29 '26 06:03

John Conde