I have the following 'key' array: (Array 1)
Array
(
[0] => first_name
[1] => surname
[2] => position
[3] => age
)
This array should determine the keys and the order of keys/values which should exist in Array 2. So in a perfect world Array 2 would look like this:
Array
(
[0] => Array
(
[first_name] => James
[surname] => Johnstone
[position] => Striker
[age] => 42
)
[1] => Array
(
[first_name] => Al
[surname] => MacLean
[position] => Defender
[age] => 22
)
...
)
The problem I'm having is in the following example array. Sometimes:
a) the order of keys in Array 2 is not the same as Array 1
b) and some of the keys defined in Array 1 don't exist in Array 2 - like so:
Array
(
[0] => Array
(
[position] => Defender
[first_name] => James
[surname] => McDonald
)
[1] => Array
(
[position] => Striker
[first_name] => Ben
[surname] => Lailey
)
...
)
I'd like some assistance creating a PHP function which will take a 'badly formed' Array 2 such as the one directly above, and convert it to how it should be: Order as defined by Array 1, and add any missing keys to become 'correct' like so:
Array
(
[0] => Array
(
[first_name] => James
[surname] => McDonald
[position] => Defender
[age] =>
)
[1] => Array
(
[first_name] => Ben
[surname] => Lailey
[position] => Striker
[age] =>
)
...
)
The keys used in this example are arbitrary, there could be a new key added, removed or re-ordered in Array 1 and I need Array 2 to respect Array 1.
Thank you in advance.