How to extract a column from an array in PHP?

Spread the love

Sometimes, we want to extract a column from an array in PHP.

In this article, we’ll look at how to extract a column from an array in PHP.

How to extract a column from an array in PHP?

To extract a column from an array in PHP, we can use the array_column function.

For instance, we write

$samples = array(
            array('page' => 'page1', 'name' => 'pagename1'),
            array('page' => 'page2', 'name' => 'pagename2'),
            array('page' => 'page3', 'name' => 'pagename3')
            );
$names = array_column($samples, 'name');

to create the $samples array.

And then we call array_column with $samples and 'name' to return an array with the values with the 'name' key in each array.

Conclusion

To extract a column from an array in PHP, we can use the array_column function.

Leave a Reply

Your email address will not be published.