PHP Array Exercises : Combine two arrays
58. Combine Two Arrays (Keys and Values)
Write a PHP script to combine (using one array for keys and another for its values) the following two arrays.
('x', 'y', 'y'), (10, 20, 30)
Sample Solution:
PHP Code:
Output:
Array ( [x] => 10 [y] => Array ( [0] => 20 [1] => 30 ) )
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to merge two arrays where one array provides keys and the other provides values, then output the resulting associative array.
- Write a PHP function to combine an array of keys and an array of values into a single associative array, handling mismatched lengths.
- Write a PHP program to use array_combine() to merge two given arrays and then validate the output structure.
- Write a PHP script to simulate the combination of two arrays without using array_combine(), by manually iterating over the elements.
Go to:
PREV : Compare Two Multidimensional Arrays for Difference.
NEXT : Create a Range-Like Array from a String.
PHP Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.