I have a multidimensional array, where I want to define the order of the keys of each subArray with an array. Let me make an example.
Input array:
$array = arr
This should work for you:
Just use array_replace() for each subArray to rearrange your elements. Use $header
as first argument and array_flip() it, so that the values are the keys, which define the order of the keys.
And each key, which is then found in the array ($header
), will be filled with the value of it (Each subArray, $v
).
As example:
Header / Key order:
Array ( [name] => [version] => [IP] => )
↑ ↑ ↑
└──┐ │ ┌─┘
┌──┼───────────┘ │
│ └───────────────────┼──┐
│ ┌───────────┘ │
| │ |
Array ( [version] => 1 [IP] => 1111 [name] => bbb )
(Each) Array:
---------------------
Result:
Array ( [name] => bbb [version] => 1 [IP] => 1111 )
Code:
<?php
$header = array("name", "version", "IP");
$array = array_map(function($v)use($header){
return array_replace(array_flip($header), $v);
}, $array);
?>