如何在PHP中反转多级数组

如何在PHP中反转多级数组,php,Php,如何简单地反转此数组,使最后一条记录显示在第一位,而其他记录将按相反顺序逐个排列 我尝试了$MyArray=array\u reverse($MyArray,true)但它只移动顶部的“状态”:“ok”,而产品数组中的所有其他记录保持不变。那么,我如何仅反转产品阵列的记录,而不反转“状态”:“ok” 您还可以使用array\u merge $json = ' { "product": [{ "IDs"

如何简单地反转此数组,使最后一条记录显示在第一位,而其他记录将按相反顺序逐个排列

我尝试了
$MyArray=array\u reverse($MyArray,true)
但它只移动顶部的“状态”:“ok”,而
产品
数组中的所有其他记录保持不变。那么,我如何仅反转
产品
阵列的记录,而不反转
“状态”:“ok”


您还可以使用array\u merge

    $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);
和输出:

{
"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"
}

$MyArray[“product”]=array_reverse($MyArray[“product”],true);你试过这个吗?顺便说一句,在显示时,你也可以反向解析数组,而不是反向读取。如果点是以反向顺序仅循环,则只从最后一个循环到第一个循环,而不是更改结构。在应用
数组反向()
之前,你是否执行了
json\u decode()
这项操作?@RK Ahir,当您
print\u r()
解码的数组时,您会看到它在顶层有两个条目,“product”和“status”
array\u reverse()
{
"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"
}