How can you merge two arrays in PHP and create a new array with one as keys and the other as values?
To merge two arrays in PHP and create a new array with one as keys and the other as values, you can use the array_combine() function. This function takes two arrays as parameters, where the first array provides keys and the second array provides values. It then returns a new array where the elements of the first array are used as keys and the elements of the second array are used as values.
$array1 = array('a', 'b', 'c');
$array2 = array(1, 2, 3);
$mergedArray = array_combine($array1, $array2);
print_r($mergedArray);
Keywords
Related Questions
- What are some alternative methods to achieve the desired functionality without relying solely on PHP?
- What are some best practices for troubleshooting and debugging regular expression patterns in PHP code?
- How can PHP developers ensure that old values are properly saved in a log when updating database records?