Environment: PHP: 5.6
I have array data get on API, I need to format it so I can generate csv file
$datas = [
[
0 => [
'age' => '18',
'name' => 'Na',
],
'info' => [
'id' => '9999',
'phone' => '012345',
]
],
[
0 => [
'age' => '20',
'name' => 'Bi',
],
'info' => [
'id' => '8888',
'phone' => '099888',
]
]
];
Array $header = ['id','name','age','phone'];
I have a code merge array
$newResults = array_map(function ($result) use($header) {
$sub = array();
$sub[] = call_user_func_array('array_merge', $result);
return $sub;
}, $datas);
Result of $newResults:
Array
(
[0] => Array
(
[0] => Array
(
[age] => 18
[name] => Na
[id] => 9999
[phone] => 012345
)
)
[1] => Array
(
[0] => Array
(
[age] => 20
[name] => Bi
[id] => 8888
[phone] => 099888
)
)
)
But this result is not the desired result, I want to sort the array $sub in the correct order of $header My desired result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 9999
[name] => Na
[age] => 18
[phone] => 012345
)
)
[1] => Array
(
[0] => Array
(
[id] => 8888
[name] => Bi
[age] => 20
[phone] => 099888
)
)
)
I tried some sort but it didn't work Thanks everyone!