In this post we explain you How to Combine Two Arrays – One for Key and Another for Value in PHP. For do this PHP has predefined array function array_combine(). Using this function combined array is return.
Combine Two Arrays – One for Key and Another for Value
Syntax: array_combine ( array $keys , array $values )
First array parameter $keys has array of keys and second array parameter array of values.
Code Example:
1 2 3 4 5 6 7 8 9 10 |
<?php $array_one = array('one', 'two', 'three'); $array_two = array('avocado', 'apple', 'banana'); $result = array_combine($array_one, $array_two); print_r($result); ?> |
Output of above example is like that:
1 2 3 4 5 6 |
Array ( [one] => avocado [two] => apple [three] => banana ) |
Note: If the array of element of both are not equal than its return FALSE result.
Using this array function you can combine two arrays by using one array for keys and other one for its values. If you like this post, Please share this with your friends. Thanks