Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP extract values from multidimensional array without a loop

I'm just wondering if it's possible to extract values from an array, in PHP, without having to loop through. Here's part of my array so you know what I mean;

Array
(
[0] => Array
    (
        [0] => 76
        [1] => Adventures of Huckleberry Finn
    )

[1] => Array
    (
        [0] => 65
        [1] => Adventures of Huckleberry Finn
    )

[2] => Array
    (
        [0] => 59
        [1] => Bookcases
    )
)

I'm after the integer [0] from each array - is there a function that can do this quicker than a foreach loop (the way I normally do this) ?

Thanks.

like image 617
Dan Avatar asked Dec 26 '22 06:12

Dan


1 Answers

Sure you are looking for the array_column function.

$newArray = array_column($originalArray,'0');

As said in PHP Manual, with this function you can extract, from an array of arrays, the only information you need. It is normally used with Associative arrays, but you can use a number as parameter and it will work as well.

like image 79
Liz Avatar answered Jan 04 '23 23:01

Liz