PHP Programing language

array_column() Function in PHP
Previous Home Next

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

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: