array_column(): Values of a column of an array

We will get records from a database in this format.
$input_array=array( 
array ( 'id' => 1,
'name' => "John Deo",
'class'=>"Four",
'mark'=>"75",
'sex'=>"female" ), 

array ( 'id' => 2,
'name' => "Max Ruin",
'class'=>"Three",
'mark'=>"85",
'sex'=>"male" ), 

array ( 'id' => 3,
'name' => "Arnold",
'class'=>"Three",
'mark'=>"55",
'sex'=>"male" ), 

array ( 'id' => 4,
'name' => "Krish Star",
'class'=>"Four",
'mark'=>"60",
'sex'=>"female" )
);
$names = array_column($input_array, 'name');
print_r($names);
Output is here
Array ( [0] => John Deo [1] => Max Ruin [2] => Arnold [3] => Krish Star )
Syntax
array_column ($input_array ,mixed $column_key [, mixed $index_key = NULL ] )
ParameterDESCRIPTION
$input_arrayRequired : Input array of which column values will be returned.
$column_keysRequired : Key of the column which values will be returned
$index_keyOptional : Value of the column which will be used as index / key for the $column_keys in the returned array
Returns an array with values of single column of the input array ( $input_array ).

Example 2 with $index_key

We will change the last line like this
$names = array_column($input_array, 'name','id');
print_r($names);
Output is here
Array ( [1] => John Deo [2] => Max Ruin [3] => Arnold [4] => Krish Star )

Example 3

Data from a mysql database table is taken and array_column() is used to display name column only.
require "config.php";// Database connection file.
$php_data_array = Array();
$result_set = $connection->query("SELECT * FROM student limit 0,4 ");// Generate resultset

while($row = $result_set->fetch_array(MYSQLI_ASSOC)){
//echo $row['id'],$row['name'],$row['class'],$row['mark']."<br>";
$php_data_array[] = $row; // Adding to array
}

$output = array_column($php_data_array,'name');
echo print_r($output);
Output is here
array()Array ( [0] => John Deo [1] => Max Ruin [2] => Arnold [3] => Krish Star ) 1

More on mysqli_fetch_array() and collecting data

Transferring PHP array data to JavaScript Array

echo "<script>var my_js_data_array= ".json_encode($php_data_array)."</script>";
In the client side JavaScript we can display the data of one column only using the same JavaScript variable.
<script>
for(var i=0; i<my_js_data_array.length; i++){
document.write(my_js_data_array[i]['class']); // List all class column 
}
</script>

Array REFERENCE Getting keys of an array
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer