Php array column vs array php code example
PHP array_column() Function, Description. array. Required. Specifies the multi-dimensional array (record-set) to use. As of PHP 7.0, this can also be an array of objects. column_key. Required. An integer key or a string key name of the column of values to return. This parameter can also be NULL to return complete arrays (useful together with index_key to re … Code sample$last_names = array_column($a, ‘last_name’);print_r($last_names);Feedback
PHP Array_Column() Function
If you want to return the columns from an array (nested array), array_column() is used. Let’s look at the following syntax:
array_column(array_input,col_key,index_key)
It takes three parameters.
Parameters:
- Array_input is the input array that has keys and values.
- The second parameter specifies the col_key that specifies the key (column name) to return the column from the array_input.
- Index_key acts as an index for the returned values in a column.
Returning Format:
Array holds the data in a linear fashion. It holds multiple elements at a time.
Array() is used to create an array in PHP. A key can refer to a value by using the => operator.
Syntax:
array(Key=>Value,……..)
Similarly, nested array means an array inside an array. It contains columns.
To display the entire array, we can use the print_r() function.
It returns the array in a format such that the key is placed inside the [] followed by a value.
Nested Array:
Let’s create an array named Flower with two nested arrays and four key-value pairs each.
//create an array named — Flower and create 2 arrays with 4 key value pairs
$Flower = array (
array (
‘Flower_name’ => ‘Lotus’ ,
‘Flower_area’ => ‘Water’ ,
‘Flower_sepals’ => 4 ,
‘Flower_petals’ => 5
) ,
array (
‘Flower_name’ => ‘lilly’ ,
‘Flower_area’ => ‘land’ ,
‘Flower_sepals’ => 2 ,
‘Flower_petals’ => 5
)
Example 1:
In this example, we will get the column names from the “Flower_name” column.
//create an array named — Flower and create 2 arrays with 4 key value pairs
$Flower = array (
array (
‘Flower_name’ => ‘Lotus’ ,
‘Flower_area’ => ‘Water’ ,
‘Flower_sepals’ => 4 ,
‘Flower_petals’ => 5
) ,
array (
‘Flower_name’ => ‘lilly’ ,
‘Flower_area’ => ‘land’ ,
‘Flower_sepals’ => 2 ,
‘Flower_petals’ => 5
)
//get the Flower_name column values
print_r ( array_column ( $Flower , ‘Flower_name’ ) ) ;
?>
Output:
We can see that values from the “Flower_name” column were returned. Also, the keys by default are assigned to them since we didn’t specify the “index_key”.
Example 2:
In this example, we will get the column names from the “Flower_area” column with the “Flower_sepals” as the index.
//create an array named — Flower and create 2 arrays with 4 key value pairs
$Flower = array (
array (
‘Flower_name’ => ‘Lotus’ ,
‘Flower_area’ => ‘Water’ ,
‘Flower_sepals’ => 4 ,
‘Flower_petals’ => 5
) ,
array (
‘Flower_name’ => ‘lilly’ ,
‘Flower_area’ => ‘land’ ,
‘Flower_sepals’ => 2 ,
‘Flower_petals’ => 5
)
//get the Flower_area column values by setting Flower_sepals index
print_r ( array_column ( $Flower , ‘Flower_area’ , ‘Flower_sepals’ ) ) ;
?>
We can see that values from the “Flower_area” column were returned. Also, the keys are assigned to the “Flower_area” column from the “Flower_sepals” column values.
Example 3:
In this example, we will get the column names from the “Flower_petals” column with the ‘Flower_area” as the index.
//create an array named — Flower and create 2 arrays with 4 key value pairs
$Flower = array (
array (
‘Flower_name’ => ‘Lotus’ ,
‘Flower_area’ => ‘Water’ ,
‘Flower_sepals’ => 4 ,
‘Flower_petals’ => 5
) ,
array (
‘Flower_name’ => ‘lilly’ ,
‘Flower_area’ => ‘land’ ,
‘Flower_sepals’ => 2 ,
‘Flower_petals’ => 5
)
//get the Flower_petals column values by setting Flower_area index
print_r ( array_column ( $Flower , ‘Flower_petals’ , ‘Flower_area’ ) ) ;
?>
Output:
We can see that the values from the “Flower_sepals” column were returned. Also, the keys are assigned to the “Flower_sepals” column from the “Flower_area” column values.
Conclusion
From this article, we learned how to get the columns from an array using the array_column() function in PHP. It is possible to set the keys to the returned values in a column from the values in another column.
PHP Arrays, In PHP, the array () function is used to create an array: array (); In PHP, there are three types of arrays: Indexed arrays — Arrays with a numeric index. Associative arrays — Arrays with named keys. Multidimensional arrays — …
Adding columns to existing php arrays
Loop through the array by reference and add what you want after the while loop:
foreach( $output as &$row) < $row->cls = 0; $row->parentID = 1; >
You can also do this within the while loop:
while($row = $result->fetch_object()) < $row->cls = 0; $row->parentID = 1; $output[] = $row; >
Solution 2
Since you changed the code snippet in your question, try this instead now (updated version):
while(. ) < $row->cls = . ; $row->parentID = . ; $output[] = $row; >
Solution 3
$myArray=array_merge($myArray,$myAddArray);
apply it in the foreach/while loop for each row.
Related videos on Youtube
Elcid_91
Comments
Using PHP let’s assume that I have successfully read a record from a MySQL table using the fetch_object method and I am holding the row data in a variable call $output:
while($row = $result->fetch_object())
If I wanted to add two additional fields: «cls» and «parentID» to $output as if they were apart of $row, how would I accomplish this? Thanks!