I am trying to dynamically merge multiple arrays. Consider below three different array inputs:
{
"container_id": [
{
"key_0": "NYKU3922051"
},
{
"key_0": "LACU3922051"
}
],
"out_ref": [
{
"key_0": "CI84621"
},
{
"key_0": "DI1000"
}
],
"in_ref2": [
{
"key_0": "555-1106260-024"
},
{
"key_0": "898-1106260-024"
}
]
}
I am trying to dynamically merge above three arrays container_id, out_ref and in_ref2 into one associative one like this:
{
"array_one": {
"key_0": "NYKU3922051",
"key_0": "CI84621",
"key_0": "555-1106260-024"
},
"array_two": {
"key_0": "LACU3922051",
"key_0": "DI1000",
"key_0": "898-1106260-024"
}
}
So what we have above, is that the first item of each array, is merged into a new array.
I am not quite sure where to start, as I can' really wrap my head around this one.
This is my PHP:
$arrays = $request->all();
$newArray = [];
foreach ($arrays as $key => $value) {
//???
}
EDIT The new array doesn't necessarily need to use the same key if this is not possible.
key_0. It can just as well be:key_0,key_1,key_2