How to rename sub-array keys in PHP?
You can use the array_combine
function in PHP to create a new array by using one array for the keys and another for the values. Here's an example of how you can use array_combine
to rename the keys of a sub-array:
<?php
$subarray = ['a', 'b', 'c'];
$new_keys = ['x', 'y', 'z'];
$renamed_array = array_combine($new_keys, $subarray);
print_r($renamed_array);
This will output:
Array ( [x] => a [y] => b [z] => c )
You can then use this $renamed_array
in place of the original sub-array.
Note that array_combine
requires that the two input arrays have the same number of elements. If the arrays have different lengths, array_combine
will return FALSE
.