Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于另一个JSON编码数组对JSON编码数组进行排序_Php_Arrays - Fatal编程技术网

Php 基于另一个JSON编码数组对JSON编码数组进行排序

Php 基于另一个JSON编码数组对JSON编码数组进行排序,php,arrays,Php,Arrays,我有两个JSON编码的数组 第一阵列 { “用户id”:2, “种子”:[“11”、“22”、“31”、“14”] } 第二阵列 { "Seeds": [ { "Team_name": "Belmont Bruins", "Team_id": "22", }, { "Team_name": "Arkansas State Red Wolves", "Team_id": "14", }, {

我有两个JSON编码的数组 第一阵列

{
“用户id”:2, “种子”:[“11”、“22”、“31”、“14”] }

第二阵列

{
"Seeds": [
    {
        "Team_name": "Belmont Bruins",
        "Team_id": "22",

    },
    {
        "Team_name": "Arkansas State Red Wolves",
        "Team_id": "14",
    },
    {
        "Team_name": "Arizona Wildcats",
        "Team_id": "11",
    },
    {
        "Team_name": "Brown Bears",
        "Team_id": "31",
    }
]
}

现在我需要根据第一个数组对第二个数组进行排序。第一个数组中的“种子”对应于第二个数组中的“团队id”。 所需输出为:

{
    "Seeds": [
        {
            "Team_name": "Arizona Wildcats",
            "Team_id": "11",

        },
        {
            "Team_name": "Belmont Bruins",
            "Team_id": "22",
        },
        {
            "Team_name": "Brown Bears",
            "Team_id": "31",
        },
        {
            "Team_name": "Arkansas State Red Wolves",
            "Team_id": "14",
        }
    ]
}

我发现了类似的问题。但在这种情况下,解决方案似乎不起作用

您应该解码json格式,对数组排序,然后编码为json格式:

$array1 = json_decode($array1_json);
$array2 = json_decode($array2_json);    
foreach ($array1['Seeds'] as $order)
    {
        foreach ($array2['Seeds'] as $data)
        {
            if ($data['Team_id'] == $order)
                $array_sorted[] = $data;
        }
    }
    $array2_sorted = array('Seeds' => $array_sorted);
    echo json_encode($array2_sorted);

您可以使用以下示例。我使用
array\u flip()
创建一个查找,并根据它对第二个数组进行排序

有了这个json:

$j1 = <<<EOF
{
   "User_id":2,
   "Seeds":["11","22","31","14"]
}
EOF;

// I needed to remove addtional commas here
$j2 = <<<EOF
{
"Seeds": [
    {
        "Team_name": "Belmont Bruins",
        "Team_id": "22"

    },
    {
        "Team_name": "Arkansas State Red Wolves",
        "Team_id": "14"
    },
    {
        "Team_name": "Arizona Wildcats",
        "Team_id": "11"
    },
    {
        "Team_name": "Brown Bears",
        "Team_id": "31"
    }
]
}
EOF;

$j1=您可以这样做:)


如何形成这两个数组?你能不能也放一些PHP代码?你能粘贴你的代码吗?JSON不重要,你只需将JSON解码成数组,然后再编码回来;不能对“JSON”排序,因为它只是一个字符串。一旦你把它从等式中去掉,看看并尝试上面列出的方法,然后你就犯了复制粘贴错误。请参阅工作代码:。它适用于PHP5.3.0的所有版本。请注意,我正在将
true
作为第二个参数传递给
json\u decode()
。我遇到了致命错误:无法将stdClass类型的对象用作
$j1 = <<<EOF
{
   "User_id":2,
   "Seeds":["11","22","31","14"]
}
EOF;

// I needed to remove addtional commas here
$j2 = <<<EOF
{
"Seeds": [
    {
        "Team_name": "Belmont Bruins",
        "Team_id": "22"

    },
    {
        "Team_name": "Arkansas State Red Wolves",
        "Team_id": "14"
    },
    {
        "Team_name": "Arizona Wildcats",
        "Team_id": "11"
    },
    {
        "Team_name": "Brown Bears",
        "Team_id": "31"
    }
]
}
EOF;
// Convert to array
$a1 = json_decode($j1, true);
$a2 = json_decode($j2, true);

// Create lookup
$lookup = array_flip($a1["Seeds"]);

// sort array2
usort($a2["Seeds"], function($a, $b) use($lookup) {
    if($lookup[$a["Team_id"]] > $lookup[$b["Team_id"]]) {
        return 1;
    } else if ($lookup[$a["Team_id"]] < $lookup[$b["Team_id"]]) {
        return -1;
    }
    return 0;
});

// Done
var_dump($a2);
$a1 = json_decode('{
   "User_id":2, 
   "Seeds":["11","22","31","14"]
}', true);

$a2 = json_decode('{"Seeds": [
    {
        "Team_name": "Belmont Bruins",
        "Team_id": "22"
    },
    {
        "Team_name": "Arkansas State Red Wolves",
        "Team_id": "14"
    },
    {
        "Team_name": "Arizona Wildcats",
        "Team_id": "11"
    },
    {
        "Team_name": "Brown Bears",
        "Team_id": "31"
    }]}', true);    

$hash = array();
$out = array();
foreach ($a2['Seeds'] as $props) $hash[$props['Team_id']] = $props;
foreach ($a1['Seeds'] as $id) $out[] = $hash[$id];
die(json_encode($out));