In this post we will be creating a PHP script that will combine two existing arrays, one of the arrays will be used as keys and the other as values. Each key will have its own corresponding value, to do this we will be using the ‘array_combine()’ function.
See below our two arrays for this example where one consists of names and the other the persons age.
$name = array("James", "Rahul", "Amy", "Leah");
$age = array("23", "24", "28", ";22");
To combine the two arrays above we would use the snippet of code below.
<?php
$name = array("James", "Rahul", "Amy", "Leah");
$age = array("23", "24", "28", ";22");
$array_combined = array_combine($name, $age);
print_r($array_combined);
?>
The above would return the following as output.
Array ( [James] => 23 [Rahul] => 24 [Amy] => 28 [Leah] => ;22 )