How to merge one array with multple elements but the same index into on array with all index in PHP. I have one array look like this and i want to merge into one array.
JavaScript
x
data": [
[
{
"req_recid": "REQ_0000000001",
"req_name": "user doer",
"req_branch": "user",
"req_position": "userdoer",
"req_from": "ho",
"req_tpye": "1",
"req_date": "Sat, Dec 12, 2020 7:36 PM"
}
],
[
{
"req_recid": "REQ_0000000004",
"req_name": "Test",
"req_branch": "Test",
"req_position": "Test",
"req_from": "ho",
"req_tpye": "4",
"req_date": "Sun, Dec 13, 2020 12:47 PM"
}
]
]
and my expected result i want like this
JavaScript
data": [
[
{
"req_recid": "REQ_0000000001",
"req_name": "user doer",
"req_branch": "user",
"req_position": "userdoer",
"req_from": "ho",
"req_tpye": "1",
"req_date": "Sat, Dec 12, 2020 7:36 PM"
},
{
"req_recid": "REQ_0000000004",
"req_name": "Test",
"req_branch": "Test",
"req_position": "Test",
"req_from": "ho",
"req_tpye": "4",
"req_date": "Sun, Dec 13, 2020 12:47 PM"
}
]
]
Anyone can help in PHP please.
Advertisement
Answer
Since you want to change a multi-dimensional array to single dimension. Laravel
has a beautiful collection method as flatten()
. This flattens your multi dimensional array to single array. In your case you can use:
JavaScript
$result = collect($data)->flatten();
to get the desired result.
Know more about it: https://laravel.com/docs/8.x/collections#method-flatten