How to use array_column() function in PHP?
The array_column() function in php returns the values from a single column in the input array.
1. Code:
<?php function getValuesColumnInArray($array, $columnName){ $values = array_column($array, $columnName); return $values; } $a = array( array( 'id' => 5698, 'first_name' => 'Chocolate', 'last_name' => 'kitkat', ), array( 'id' => 4767, 'first_name' => 'Bike', 'last_name' => 'Activa', ), array( 'id' => 3809, 'first_name' => 'Mobile', 'last_name' => 'Oppo', ) ); $values = getValuesColumnInArray($a, 'last_name'); print_r($values); ?>
2. Example:
Input:
$array = array( array( 'id' => 5698, 'first_name' => 'Chocolate', 'last_name' => 'kitkat', ), array( 'id' => 4767, 'first_name' => 'Bike', 'last_name' => 'Activa', ), array( 'id' => 3809, 'first_name' => 'Mobile', 'last_name' => 'Oppo', ) ); column name = last_name
Output:
Array ( [0] => kitkat [1] => Activa [2] => Oppo )