PHP Programing language

array_column() Function in PHP
Previous Home Next
adplus-dvertising

array_column()

The array_column() function returns the values from a single column in the input array.

Syntax:
array_column(array,column_key,index_key);

In above syntax,"array" specifies the multi-dimensional array (record-set) to use, "column_key" an integer key or a string key name of the column of values to return and "index_key" index/keys returns array

Example:

<?php
$fruit_weight = array(
  array(
    'id' => 1234,
    'fruit' => 'Apple',
    'weight' => '5kg',
  ),
  array(
    'id' => 5678,
    'fruit' => 'Banana',
    'weight' => '7kg',
  ),
  array(
    'id' => 9012,
    'fruit' => 'Mango',
    'weight' => '10kg',
  )
);

$fruit_names = array_column($fruit_weight, 'fruit', 'id');
print_r($fruit_names);
?>

Output:

Array ( [1234] => Apple [5678] => Banana [9012] => Mango )

Previous Home Next