How to reverse multilevel array in PHP

How to simply reverse this array so last record displays first and other records will arrange one by one in reverse order.
I tried$MyArray=array_reverse($MyArray,true);
but it only moves"status": "ok"
at top and all other records insideproducts
array remain unchanged. So how can I reverse records ofproducts
array only and not"status": "ok"
?
{
"product": [{
"IDs": ["00000087102110"],
"Brand": "SONY",
"Rank": 1
}, {
"IDs": ["00000087102120"],
"Brand": "SAMSUNG",
"Rank": 1
}, {
"GPI14s": ["00000087102150"],
"Brand": "HCL",
"Rank": 1
}, {
"GPI14s": ["00000087102110"],
"Brand": "LG",
"Rank": 1
}, {
"GPI14s": ["00000087102120"],
"Brand": "LENOVO",
"Rank": 1
}, {
"GPI14s": ["00000087102150"],
"Brand": "HP",
"Rank": 1
}],
"status": "ok"
}
Answer
Solution:
You can use array_merge also
$json = '
{
"product": [{
"IDs": ["00000087102110"],
"Brand": "SONY",
"Rank": 1
}, {
"IDs": ["00000087102120"],
"Brand": "SAMSUNG",
"Rank": 1
}, {
"GPI14s": ["00000087102150"],
"Brand": "HCL",
"Rank": 1
}, {
"GPI14s": ["00000087102110"],
"Brand": "LG",
"Rank": 1
}, {
"GPI14s": ["00000087102120"],
"Brand": "LENOVO",
"Rank": 1
}, {
"GPI14s": ["00000087102150"],
"Brand": "HP",
"Rank": 1
}],
"status": "ok"
}';
$decoded = json_decode($json,true);
$reverse['product'] = array_reverse($decoded['product']);
unset($decoded['product']);
$reverse = array_merge($reverse,$decoded);
echo json_encode($reverse,JSON_PRETTY_PRINT);
and output with :
{
"product": [
{
"GPI14s": [
"00000087102150"
],
"Brand": "HP",
"Rank": 1
},
{
"GPI14s": [
"00000087102120"
],
"Brand": "LENOVO",
"Rank": 1
},
{
"GPI14s": [
"00000087102110"
],
"Brand": "LG",
"Rank": 1
},
{
"GPI14s": [
"00000087102150"
],
"Brand": "HCL",
"Rank": 1
},
{
"IDs": [
"00000087102120"
],
"Brand": "SAMSUNG",
"Rank": 1
},
{
"IDs": [
"00000087102110"
],
"Brand": "SONY",
"Rank": 1
}
],
"status": "ok"
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: zsh: command not found: php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.