w3resource

PHP : array_combine() function

PHP: Combine array keys and values

The array_combine() function is used to creates an array by using one array for keys and another for its values.

Version:

(PHP 4 and above)

Syntax:

array_combine(array_keys, array_values)

Parameter:

Name Description Required /
Optional
Type
array_keys The keys of the array to be used. Required Array
array_values The values of the array to be used. Required Array

Return value:

The combined array, FALSE if the arrays are empty or the number of elements for each array isn't equal.

Value Type : Array

Example :

<?php
$subject =array('Language', 'Math', 'Science', 'Geography');
$marks = array(77, 80, 90, 75);
$subject_marks=array_combine($subject, $marks);
print_r($subject_marks)
?>

Output:

Array ([Language] => 77 [Math] => 80 [Science] => 90 [Geography] => 75)

Pictorial Presentation:

php function reference: array_combine() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous:array_chunk
Next: array_count_values



PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);