$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 ] )
Parameter | DESCRIPTION |
---|---|
$input_array | Required : Input array of which column values will be returned. |
$column_keys | Required : Key of the column which values will be returned |
$index_key | Optional : Value of the column which will be used as index / key for the $column_keys in the returned array |
$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 )
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
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>