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
(
  [key] => value
  …..
  …..
)

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.

Array
(
[Key] => Value
……………
……………
)

Nested Array:

Array
(
Array
(
[Key] => Value
……………
……………
).
……………
……………
)

Let’s create an array named Flower with two nested arrays and four key-value pairs each.

<?php

//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
        )
 
);


print_r($Flower);
?>

Output:

Example 1:

In this example, we will get the column names from the “Flower_name” column.

<?php

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

<?php

//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'));
?>

Output:

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.

<?php

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



from https://ift.tt/69ek81R

Post a Comment

0 Comments