Php add column to array

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:

  1. Array_input is the input array that has keys and values.
  2. The second parameter specifies the col_key that specifies the key (column name) to return the column from the array_input.
  3. 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.

Читайте также:  Apache commons exec java

//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.

JQuery Javascript Tutorial | Convert Static HTML Table Into PHP Array | Backend Language Array

Array column | PHP Array Function

47: Inserting database results into array in PHP - PHP tutorial

Adding a column to a database table in phpMyAdmin

46: Insert data into array in PHP - PHP tutorial

PHP Tutorial - 12 - Adding and Modifying Elements in an Array

Add and Change Array Elements, Add Elements to An Array in PHP, Changing Elements in An Array in PHP

PHP, Add Element to End of Array, array_push Method, Remove Element in Array with array_pop Method

how to add new column to a exising database table in phpMyAdmin

insert multiple record in single column in php using array

Elcid_91

Elcid_91

Comments

Elcid_91

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!

Читайте также:  Format date using php

Источник

Оцените статью