Convert a regular array to an associative array in PHP
This article demonstrates converting a regular array to an associative array in PHP.
1. Using array_combine() function
The array_combine() function creates an array by using the array specified as the first argument for keys and the array specified as the second argument for its values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $fruits = array('Apple', 'Banana', 'Orange'); $associative_array = array_combine(range(1, sizeof($fruits)), $fruits); print_r($associative_array); /* Output: Array ( [1] => Apple [2] => Banana [3] => Orange ) */ ?> |
Note that in PHP 8 and above, a “ValueError” is thrown if argument #1 ($keys) and argument #2 ($values) do not have the same number of elements. In older PHP versions, false is returned instead. Here’s a slightly modified version that can easily handle arrays of different sizes. It works by trimming the shorter array before invoking the array_combine() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php function combine_array($first, $second) { if (sizeof($first) > sizeof($second)) { return array_combine(array_slice($first, 0, sizeof($second)), $second); } else { return array_combine($first, array_slice($second, 0, sizeof($first))); } } $abbreviation = array("MI", "WI", "TX", "MO"); $state = array("Michigan", "Wisconsin", "Texas"); $associative_array = combine_array($abbreviation, $state); print_r($associative_array); /* Output: Array ( [MI] => Michigan [WI] => Wisconsin [TX] => Texas ) */ ?> |
If the first array has any duplicates, the associated value of the latest key is used, and all others will be ignored. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $abbreviation = array("TX", "WI", "TX"); $state = array("Not Found", "Wisconsin", "Texas"); $associative_array = array_combine($abbreviation, $state); print_r($associative_array); /* Output: Array ( [TX] => Texas [WI] => Wisconsin ) */ ?> |
2. Using array_fill_keys() function
The array_fill_keys() function creates an array using the values of the array specified in the first parameter for its keys, and the value specified in the second parameter for its values. The following example creates an associative array for storing the letter frequency.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $freq = array_fill_keys(range('a', 'z'), 0); print_r($freq); /* Output: Array ( [a] => 0 [b] => 0 [c] => 0 ... ... [z] => 0 ) */ ?> |
3. Using foreach loop
Another option is to loop through the regular array and insert each value into the associative array manually. For example, the following snippet creates an associative array of points table and initializes it using a foreach loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $array = array('Brazil', 'France', 'Argentina', 'Germany'); $initial_val = 0; $associative_array = []; foreach ($array as $value) { $associative_array[$value] = $initial_val; } print_r($associative_array); /* Output: Array ( [Brazil] => 0 [France] => 0 [Argentina] => 0 [Germany] => 0 ) */ ?> |
4. Using array_flip() function
Finally, you can use the array_flip() function, which swaps all keys in an array with their corresponding values. The following is a simple example demonstrating the usage of the array_flip() function on a numeric array. It creates a new array in reverse order, i.e. keys from the original array become values, and the values become keys.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $fruits = array('Apple', 'Banana', 'Orange'); $associative_array = array_flip($fruits); print_r($associative_array); /* Output: Array ( [Apple] => 0 [Banana] => 1 [Orange] => 2 ) */ ?> |
Note that if a value has several occurrences, the later key of two duplicates is preserved and the earlier one is lost:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $array = array("MI", "WI", "MI", "TX"); $associative_array = array_flip($array); print_r($associative_array); /* Output: Array ( [MI] => 2 [WI] => 1 [TX] => 3 ) */ ?> |
That’s all about converting a regular array to an associative array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)