PHP array combine

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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

Pictorial Presentation :

php-array_combine()

Example :

  1. $subject =array('Language', 'Math', 'Science', 'Geography');  

  2. $marks = array(77, 80, 90, 75);  

  3. $subject_marks=array_combine($subject, $marks);  

  4. print_r($subject_marks)  

  5. ?>  

Output :

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