PHP array_combine() Function

What is the PHP array_combine() Function?

The PHP array_combine() function is used to create an array using two arrays.

Both arrays must have the same length of elements.


Syntax of the array_combine()

array_combine(first_array, second_array);
  • The PHP array combine function takes two parameters –
    1. first_array – The first array will be used for array keys.
    2. second_array – The second array will be used for array values.

Example:

<?php
$name = array("John", "Mark", "Barry");
$age = array(25, 21, 18);

$combine = array_combine($name,$age);

echo "<pre>";
var_dump($combine);
echo "</pre>";
?>
Browser Output
array(3) {
  ["John"]=>
  int(25)
  ["Mark"]=>
  int(21)
  ["Barry"]=>
  int(18)
}

Leave a Reply

Your email address will not be published. Required fields are marked *