Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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对象进行排序_Php_Sorting - Fatal编程技术网

无法使用PHP按相同键对json对象进行排序

无法使用PHP按相同键对json对象进行排序,php,sorting,Php,Sorting,我试图按相同的键对json对象进行排序,并使用PHP合并值部分,但没有得到预期的结果。我的代码如下: $customArr=[{"attribute_code":"budget","value":"141"},{"attribute_code":"restaurants","value":"166"},{"attribute_code":"food_type","value":"172"},{"attribute_code":"budget","value":"142"},{"attribute

我试图按相同的键对json对象进行排序,并使用PHP合并值部分,但没有得到预期的结果。我的代码如下:

$customArr=[{"attribute_code":"budget","value":"141"},{"attribute_code":"restaurants","value":"166"},{"attribute_code":"food_type","value":"172"},{"attribute_code":"budget","value":"142"},{"attribute_code":"restaurants","value":"168"},{"attribute_code":"food_type","value":"173"},{"attribute_code":"budget","value":"143"},{"attribute_code":"restaurants","value":"170"},{"attribute_code":"food_type","value":"173"},{"attribute_code":"budget","value":"162"},{"attribute_code":"restaurants","value":"171"},{"attribute_code":"food_type","value":"172"}]
function sortByName($a, $b){
    $a = $a['attribute_code'];
    $b = $b['attribute_code'];

    if ($a == $b) return 0;
    return ($a < $b) ? -1 : 1;
}
usort($customArr, 'sortByName');

但是在我的例子中,预期的结果不会出现。

您有一个JSON字符串,而不是数组

$jsonString = '[{"attribute_code":"budget","value":"141"},{"attribute_code":"restaurants","value":"166"},{"attribute_code":"food_type","value":"172"},{"attribute_code":"budget","value":"142"},{"attribute_code":"restaurants","value":"168"},{"attribute_code":"food_type","value":"173"},{"attribute_code":"budget","value":"143"},{"attribute_code":"restaurants","value":"170"},{"attribute_code":"food_type","value":"173"},{"attribute_code":"budget","value":"162"},{"attribute_code":"restaurants","value":"171"},{"attribute_code":"food_type","value":"172"}]';
$customArr = json_decode($jsonString, true);

    function sortByName($a, $b)
    {
        $a = $a['attribute_code'];
        $b = $b['attribute_code'];

        if ($a == $b) return 0;
        return ($a < $b) ? -1 : 1;
    }

    usort($customArr, 'sortByName');
$jsonString='[{“属性代码”:“预算”、“价值”:“141”},{“属性代码”:“餐厅”、“价值”:“166”},{“属性代码”:“食品类型”、“价值”:“172”},{“属性代码”:“预算”、“价值”:“142”},{“属性代码”:“餐厅”、“价值”:“168”},{“属性代码”:“食品类型”、“价值”:“173”},{“属性代码”:“预算”、“价值”:“143:“餐厅”、“价值”:“170”}、{“属性代码”:“食品类型”、“价值”:“173”}、{“属性代码”:“预算”、“价值”:“162”}、{“属性代码”:“餐厅”、“价值”:“171”}、{“属性代码”:“食品类型”、“价值”:“172”});
$customArr=json_decode($jsonString,true);
函数sortByName($a,$b)
{
$a=$a['attribute_code'];
$b=$b['attribute_code'];
如果($a==$b)返回0;
回报率($a<$b)?-1:1;
}
usort($customArr,'sortByName');

目前,您只需根据属性代码对条目进行排序,而不需要合并数据项

此代码将创建一个新的输出数组(由属性_code为其设置关键帧),如果代码已经存在,则将值添加到现有值列表中,如果没有,则添加一个新项,将值创建为数组(第一项位于)。最后,它使用
ksort()
对项目进行排序

如果您不需要这些键,那么
array\u values()
将为您提供一个普通数组(如输出中所示)

Array
(
    [0] => Array
        (
            [attribute_code] => budget
            [value] => Array
                (
                    [0] => 141
                    [1] => 142
                    [2] => 143
                    [3] => 162
                )

        )

    [1] => Array
        (
            [attribute_code] => food_type
            [value] => Array
                (
                    [0] => 172
                    [1] => 173
                )

        )

    [2] => Array
        (
            [attribute_code] => restaurants
            [value] => Array
                (
                    [0] => 166
                    [1] => 168
                    [2] => 170
                    [3] => 171
                )

        )

)
使用
array\u reduce()
可以尝试
array_reduce()
callback接受两个参数,第一个参数是旧的/以前的迭代值,第二个参数是当前的迭代值/元素

因此,使用这个函数,我们可以将当前迭代值保持为上一个迭代值(总值)

使用回调函数迭代地将数组缩减为单个值

$customArr=json\u decode('[{“属性码”:“预算”、“价值”:“141”},{“属性码”:“餐厅”、“价值”:“166”},{“属性码”:“食品类型”、“价值”:“172”},{“属性码”:“预算”、“价值”:“142”},{“属性码”:“餐厅”、“价值”:“168”},{“属性码”:“食品类型”、“价值”:“173”},{“属性码”:“预算”、“价值”:“143{“属性代码”:“餐厅”、“价值”:“170”},{“属性代码”:“食品类型”、“价值”:“173”},{“属性代码”:“预算”、“价值”:“162”},{“属性代码”:“餐厅”、“价值”:“171”},{“属性代码”:“食品类型”、“价值”:“172”},真);
$data=array\u reduce($customArr,function($acc,$new){
如果(isset($acc[$new['attribute_code']])){
$old_value=$acc[$new['attribute_code']]['value'];
$acc[$new['attribute\u code']['value']=array\u唯一(array\u是($old\u value)?array\u merge($old\u value,[$new['value']]):[$old\u value,$new['value']);
}否则{
$acc[$new['attribute_code']]=$new;
}
返回$acc;
}, []);
ksort(数据);
echo“”,json_编码(数组_值($data));

工作。

与上述类似的解决方案,但使用temp变量和在单个迭代中略有不同:

Array
(
[0] => Array
    (
        [attribute_code] => budget
        [value] => Array
            (
                [0] => 141
                [1] => 142
                [2] => 143
                [3] => 162
            )

    )

[1] => Array
    (
        [attribute_code] => restaurants
        [value] => Array
            (
                [0] => 166
                [1] => 168
                [2] => 170
                [3] => 171
            )

    )

[2] => Array
    (
        [attribute_code] => food_type
        [value] => Array
            (
                [0] => 172
                [1] => 173
            )

    )

)
JSON编码响应


JSON是不是PHP的JavaScript对象表示法。@GentleSama:这是“JSON_encode”输出。
JSON_encode
的输出是什么?您的
$customArr
?@GentleSama:是的。这是解决方案吗?是的,它看起来不错,但仍然存在重复值。我在末尾添加了一个循环,使值也是唯一的(每次保存调用
array\u unique()
)我们可以删除一个循环,并可以使用临时变量获得类似的结果。
Array
(
    [0] => Array
        (
            [attribute_code] => budget
            [value] => Array
                (
                    [0] => 141
                    [1] => 142
                    [2] => 143
                    [3] => 162
                )

        )

    [1] => Array
        (
            [attribute_code] => food_type
            [value] => Array
                (
                    [0] => 172
                    [1] => 173
                )

        )

    [2] => Array
        (
            [attribute_code] => restaurants
            [value] => Array
                (
                    [0] => 166
                    [1] => 168
                    [2] => 170
                    [3] => 171
                )

        )

)
$customArr = json_decode('[{"attribute_code":"budget","value":"141"},{"attribute_code":"restaurants","value":"166"},{"attribute_code":"food_type","value":"172"},{"attribute_code":"budget","value":"142"},{"attribute_code":"restaurants","value":"168"},{"attribute_code":"food_type","value":"173"},{"attribute_code":"budget","value":"143"},{"attribute_code":"restaurants","value":"170"},{"attribute_code":"food_type","value":"173"},{"attribute_code":"budget","value":"162"},{"attribute_code":"restaurants","value":"171"},{"attribute_code":"food_type","value":"172"}]', true);

$data = array_reduce($customArr, function ($acc, $new) {
    if (isset($acc[$new['attribute_code']])) {
        $old_value = $acc[$new['attribute_code']]['value'];
        $acc[$new['attribute_code']]['value'] = array_unique(is_array($old_value) ? array_merge($old_value, [$new['value']]) : [$old_value, $new['value']]);
    } else {
        $acc[$new['attribute_code']] = $new;
    }
    return $acc;
}, []);

ksort($data);

echo '<pre>', json_encode(array_values($data));
<?php 
$str='[{"attribute_code":"budget","value":"141"},{"attribute_code":"restaurants","value":"166"},{"attribute_code":"food_type","value":"172"},{"attribute_code":"budget","value":"142"},{"attribute_code":"restaurants","value":"168"},{"attribute_code":"food_type","value":"173"},{"attribute_code":"budget","value":"143"},{"attribute_code":"restaurants","value":"170"},{"attribute_code":"food_type","value":"173"},{"attribute_code":"budget","value":"162"},{"attribute_code":"restaurants","value":"171"},{"attribute_code":"food_type","value":"172"}]';
$arr = json_decode($str,true);
$temp = $result= [];
foreach($arr as $key=>$customer){
    if(in_array($customer['attribute_code'], $temp)){
        $index=array_search($customer['attribute_code'],$temp);
        if(!in_array($customer['value'],$result[$index]['value'])){
            $result[$index]['value'][]=$customer['value'];
        }
    }else {
        $temp[]=$customer['attribute_code'];
        $result[]=[
            'attribute_code'=>$customer['attribute_code'],
            'value'=>[$customer['value']]
        ];
    }
}
unset($temp);
print_r($result);
echo json_encode($result);
?>
Array
(
[0] => Array
    (
        [attribute_code] => budget
        [value] => Array
            (
                [0] => 141
                [1] => 142
                [2] => 143
                [3] => 162
            )

    )

[1] => Array
    (
        [attribute_code] => restaurants
        [value] => Array
            (
                [0] => 166
                [1] => 168
                [2] => 170
                [3] => 171
            )

    )

[2] => Array
    (
        [attribute_code] => food_type
        [value] => Array
            (
                [0] => 172
                [1] => 173
            )

    )

)
  [{"attribute_code":"budget","value":["141","142","143","162"]},{"attribute_code":"restaurants","value":["166","168","170","171"]},{"attribute_code":"food_type","value":["172","173"]}]