Listado de la etiqueta: Array_Combine


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