I have an array like this
[
123456 => 'John Doe'
654321 => 'Doe John'
]
I want to change the array key (123456 and 654321) into an index (0,1,2,3,….,n) and save it into value, the expected result looks like this
array(
0 => array(
'name' => 'John Doe',
'code' => 123456.
),
1 => array(
'name' => 'Doe John',
'code' => 654321.
),
)
this is the code i have tried, i have only gotten so far
$thearray= array_map(function ($value, $key) {
return $key;
}, $thearray, array_keys($thearray));
2
Answers
Use the foreach
Within a foreach loop, define the keys and values using the keyname that you wish to see in the resultant rows, Then call
compact()
within the loops body before pushing the new row’s data into the result array.Code: (Demo)
Your functional-style programming can be fixed up like this:
Code: (Demo)