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:

View the example in the browser
Practice here online :
See also
Previous:array_chunk
Next: array_count_values
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises