Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/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_Arrays_Json_Sorting_Search - Fatal编程技术网

Php 从Json数组中删除重复项的算法

Php 从Json数组中删除重复项的算法,php,arrays,json,sorting,search,Php,Arrays,Json,Sorting,Search,我有以下JSON数据: [{"make":"Cadillac","model":"Escalade","year":"2006"},{"make":"Cadillac","model":"Escalade","year":"2005"},{"make":"Cadillac","model":"Escalade","year":"2004"},{"make":"Cadillac","model":"Escalade","year":"2003"},{"make":"Cadillac","model

我有以下JSON数据:

[{"make":"Cadillac","model":"Escalade","year":"2006"},{"make":"Cadillac","model":"Escalade","year":"2005"},{"make":"Cadillac","model":"Escalade","year":"2004"},{"make":"Cadillac","model":"Escalade","year":"2003"},{"make":"Cadillac","model":"Escalade","year":"2002"},{"make":"Cadillac","model":"Escalade ESV","year":"2006"},{"make":"Cadillac","model":"Escalade ESV","year":"2005"},{"make":"Cadillac","model":"Escalade ESV","year":"2004"},{"make":"Cadillac","model":"Escalade ESV","year":"2003"},{"make":"Cadillac","model":"Escalade EXT","year":"2006"},{"make":"Cadillac","model":"Escalade EXT","year":"2005"},{"make":"Cadillac","model":"Escalade EXT","year":"2004"},{"make":"Cadillac","model":"Escalade EXT","year":"2003"},{"make":"Cadillac","model":"Escalade EXT","year":"2002"},{"make":"Chevrolet","model":"Astro","year":"2005"},{"make":"Chevrolet","model":"Astro","year":"2004"},{"make":"Chevrolet","model":"Astro","year":"2003"},{"make":"Chevrolet","model":"Avalanche 1500","year":"2006"},{"make":"Chevrolet","model":"Avalanche 1500","year":"2005"}]
如何对数据格式进行排序

Cadillac    Escalade    07-13
Cadillac    Escalade ESV    07-13
Cadillac    Escalade EXT    07-13
Cadillac    XTS 2013
Chevrolet   Avalanche   07-13
Chevrolet   Express 1500    09-14

我对PHP还是一个新手,所以我很难弄明白如何实现这一点,如何删除重复项,以及如何通过串联增加年份


谢谢,

您必须使用循环解析它。其简单版本如下所示:

$cars = json_decode($cars_json);
$carList = array();
foreach($cars as $car){
    if(!isset($carList[$car->make . $car->model])){
        $carList[$car->make . $car->model] = array(
            'firstYear' => $car->year,
            'lastYear' => $car->year,
            'make'=>$car->make,
            'model' => $car->model,
    }
    else{
        if($car->year < $carList[$car->make . $car->model]['firstYear'])
            $carList[$car->make . $car->model]['firstYear'] = $car->year;
        else if($car->year > $carList[$car->make . $car->model]['lastYear'])
            $carList[$car->make . $car->model]['lastYear'] = $car->year;
    }
}
//Parsing complete, now output

foreach($carList as $car){
  echo $car['make'] . " ";
  echo $car['model'] . " ";
  echo substr($car['firstYear'],-2) . "-" . substr($car['lastYear'], -2);
}
$cars=json\u decode($cars\u json);
$carList=array();
foreach($cars作为$car){
如果(!isset($carList[$car->make.$car->model])){
$carList[$car->make.$car->model]=数组(
“第一年”=>$car->year,
“去年”=>$car->year,
'make'=>$car->make,
'model'=>$car->model,
}
否则{
如果($car->year<$carList[$car->make.$car->model]['firstYear'])
$carList[$car->make.$car->model]['firstYear']=$car->year;
否则如果($car->year>$carList[$car->make.$car->model]['lastYear'])
$carList[$car->make.$car->model]['lastYear']=$car->year;
}
}
//解析完成,现在输出
foreach($carList作为$car){
echo$car['make'];
echo$car['model']。“”;
echo substr($car['firstYear',-2)。“-”.substr($car['lastYear',-2);
}
产出:

Cadillac    Escalade    2002-2006
Cadillac    Escalade ESV    2003-2006
Cadillac    Escalade EXT    2002-2006
Chevrolet   Astro   2003-2005
Chevrolet   Avalanche 1500  2005-2006
$organized
是在第一个
foreach
中创建的多维数组,如下所示:

Array
(
    [Cadillac] => Array
        (
            [Escalade] => Array
                (
                    [0] => 2006
                    [1] => 2005
                    [2] => 2004
                    [3] => 2003
                    [4] => 2002
                )

            [Escalade ESV] => Array
                (
                    [0] => 2006
                    [1] => 2005
                    [2] => 2004
                    [3] => 2003
                )

            [Escalade EXT] => Array
                (
                    [0] => 2006
                    [1] => 2005
                    [2] => 2004
                    [3] => 2003
                    [4] => 2002
                )

        )

    [Chevrolet] => Array
        (
            [Astro] => Array
                (
                    [0] => 2005
                    [1] => 2004
                    [2] => 2003
                )

            [Avalanche 1500] => Array
                (
                    [0] => 2006
                    [1] => 2005
                )

        )

)

什么是
07-13
?它不会出现在你的json数据中,通过串联添加年份。这很优雅,我已经尝试了2天,你能用if语句而不是三元运算来重构吗?也许还可以解释两个foreach循环和排序发生了什么?一些额外的注释,这样我就可以真正掌握l这背后的逻辑。做得好,尽管这正是我想要完成的。@hayonj,我补充了一点细节。我不知道我是否理解“如果(计数($years))`我会在上面使用for循环,你能解释一下它的作用吗?正如你从我添加到答案中的数组转储中看到的,$years成为JSON数据中所有可用年份的数组。我使用
count($years)做什么
正在检查是否有更多的值来创建年份范围。
array\u shift
从数组中删除第一项。如果count返回任何内容,我知道我需要创建年份范围。重构前的第一段代码是:
$sorted=array\u map(函数($arr){return array\u map(函数($a){sort($a);return$a;},$arr);},$organized);
您能解释一下数组映射和匿名函数的情况吗?谢谢。
Array
(
    [Cadillac] => Array
        (
            [Escalade] => Array
                (
                    [0] => 2006
                    [1] => 2005
                    [2] => 2004
                    [3] => 2003
                    [4] => 2002
                )

            [Escalade ESV] => Array
                (
                    [0] => 2006
                    [1] => 2005
                    [2] => 2004
                    [3] => 2003
                )

            [Escalade EXT] => Array
                (
                    [0] => 2006
                    [1] => 2005
                    [2] => 2004
                    [3] => 2003
                    [4] => 2002
                )

        )

    [Chevrolet] => Array
        (
            [Astro] => Array
                (
                    [0] => 2005
                    [1] => 2004
                    [2] => 2003
                )

            [Avalanche 1500] => Array
                (
                    [0] => 2006
                    [1] => 2005
                )

        )

)