Archive d’étiquettes pour : PHP


If you want to combine two arrays, the array_combine() function is the best option. It combines the two arrays, such that the first array acts as the key and the second array acts as the values with respect to the keys in the first array. Hence, it is important to have the same number of elements in both arrays.

Syntax:
array_combine(array_input1,array_input2)

It takes two parameters.

Parameters:

  1. array_input1 is the first input array
  2. array_input2 is the second input array

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.

Syntax:
array(Value,……..)

To display the entire array, we can use the print_r() function.

Array
(
Value1,
……………
……………
)

Let’s create two arrays having four values each.

<?php

//create an array named – Flower that has 4 values
$Flower=array(‘Flower_name’,‘Flower_area’,‘Flower_sepals’,‘Flower_petals’);
 
print_r($Flower);

//create an array named – Flower that has 4 values
$Type=array(‘lotus’,‘water’,4,5);

print_r($Type);

?>

Output:

The first array is “Flower”. It has four values – “Flower_name”, “Flower_area”, “Flower_sepals”, and “Flower_petals”. The second array is “Type” and has 4 values – “lotus”, “water” , “4”, and “5”.

Now, we combine these two arrays.

Example 1:

In this example, we will combine the “Flower” and “Type” arrays.

<?php

//create an array named – Flower that has 4 values
$Flower=array(‘Flower_name’,‘Flower_area’,‘Flower_sepals’,‘Flower_petals’);
 
//create an array named – Flower that has 4 values
$Type=array(‘lotus’,‘water’,4,5);

//combine $Flower and $Type
print_r(array_combine($Flower,$Type));
?>

Output:

We can see that both the arrays are combined. The values in the first array are placed as keys and the values in the second array are placed as value with respect to key.

If we want to combine the arrays that have two key-value pairs, it combines the values from both the arrays such that the values in the first array act as key and the values in the second array act as value in the combined array.

Example 2:

<?php

//create an array named – Flower1 that has 4 values
$Flower1=array(‘Flower_name’=>‘lotus’,‘Flower_area’=>‘water’,‘Flower_sepals’=>4,‘Flower_petals’=>5);
 
//create an array named – Flower2 that has 4 values
$Flower2=array(‘Flower_name’=>‘lilly’,‘Flower_area’=>‘land’,‘Flower_sepals’=>2,‘Flower_petals’=>6);

//combine $Flower1 and $Flower2
print_r(array_combine($Flower1,$Flower2));
?>

Output:

We can see that both the arrays are combined. The values in the first array are placed as keys and the values in the second array are placed as value with respect to key.

Example 3:

In this example, we will demonstrate the error with a different number of elements.

<?php

//create an array named – Flower that has 5 values
$Flower=array(‘Flower_name’,‘Flower_area’,‘Flower_sepals’,‘Flower_petals’,‘flower_cost’);
 
//create an array named – Flower that has 4 values
$Type=array(‘lotus’,‘water’,4,5);

//combine $Flower and $Type
print_r(array_combine($Flower,$Type));
?>

Output:

We can see that an error occurs if we combine the two arrays with different number of elements. The first array has five elements and the second array has four elements. Hence, two arrays are not combined.

Conclusion

We discussed how to combine the two arrays using the array_combine() function. It returns a key-value pair combined array from the two arrays by taking the two arrays as parameters. Just note that the total number of elements in both the elements must be the same.



Source link


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.



Source link


If you want to convert your array keys into the uppercase or lowercase in PHP, array_change_key_case() is used to convert the keys in an array into the Upper case or Lower case.

Let’s see the following syntax:
array_change_key_case(array_input,CASE_LOWER/CASE_UPPER)

It takes two parameters.

Parameters:

  1. Array_input is the input array that has keys and values.
  2. The second parameter specifies the case in which keys are converted. It takes two possible values:

CASE_LOWER converts all the keys present in the array to lower.

CASE_UPPER converts all the keys present in the array to upper.

By default, the keys are converted into lowercase.

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,……..)

To display the entire array, we can use the print_r() function. It returns the array in the format such that the key is placed inside the [] followed by the value.

Array

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

Let’s create an array with four keys and values.

<?php
//create an array named – Flower with four keys and values
$Flower=array(«Flower_name»=>«Lotus»,«Flower_area»=>«water»,«Flower_sepals»=>10,«Flower_petals»=>20);

//display the Flower array
print_r($Flower);
?>

Output:

You can see that the keys are “Flower_name”, “Flower_area”, “Flower_sepals, and “Flower_petals”.

Now, we convert them into the upper/lower case using the function.

Example 1:

In this example, we will use the array_change_key_case() function to convert the keys in the array to lowercase without specifying a second parameter.

<?php
//create an array named – Flower with four keys and values
$Flower=array(«Flower_name»=>«Lotus»,«Flower_area»=>«water»,«Flower_sepals»=>10,«Flower_petals»=>20);

//display the Flower array by converting the keys into lowercase
print_r(array_change_key_case($Flower));
?>

Output:

You can observe that all the four keys in the array are converted to lowercase.

Example 2:

In this example, we will use the array_change_key_case() function to convert the keys in the array to lowercase by specifying a second parameter – CASE_LOWER.

<?php
//create an array named – Flower with four keys and values
$Flower=array(«Flower_name»=>«Lotus»,«Flower_area»=>«water»,«Flower_sepals»=>10,«Flower_petals»=>20);

//display the Flower array by converting the keys into lowercase
print_r(array_change_key_case($Flower,CASE_LOWER));
?>

Output:

You can observe that all the four keys in the array are converted to lowercase.

Example 3:

In this example, we will use the array_change_key_case() function to convert the keys in the array to uppercase by specifying a second parameter – CASE_UPPER.

<?php
//create an array named – Flower with four keys and values
$Flower=array(«Flower_name»=>«Lotus»,«Flower_area»=>«water»,«Flower_sepals»=>10,«Flower_petals»=>20);

//display the Flower array by converting the keys into uppercase
print_r(array_change_key_case($Flower,CASE_UPPER));
?>

Output:

You can observe that all the four keys in the array are converted to uppercase.

Conclusion

From this PHP tutorial, we learned how to convert all the keys in an array to lower/upper case using the array_key_case() function. It is a built-in function that takes the CASE_LOWER as a second parameter to convert the keys to lowercase and takes the CASE_UPPER to convert the keys to uppercase. If we don’t specify the second parameter, it converts into lowercase.



Source link