Php 在json数组中搜索重复的classid

Php 在json数组中搜索重复的classid,php,json,Php,Json,我试图在json数组中搜索重复的ClassID,对于找到的每个重复项,回显dulicate id。。。这只是json文件的一个示例 我尝试了一些方法,但是失败了——如果我发布了我的代码,那么这个示例代码就不起作用了。在我检查另一个json文件以查找匹配的ID之后,它要复杂得多。。。还有一堆其他的东西 提前谢谢 { "response": { "received": [ { "items": [ {

我试图在json数组中搜索重复的ClassID,对于找到的每个重复项,回显dulicate id。。。这只是json文件的一个示例

我尝试了一些方法,但是失败了——如果我发布了我的代码,那么这个示例代码就不起作用了。在我检查另一个json文件以查找匹配的ID之后,它要复杂得多。。。还有一堆其他的东西

提前谢谢

{
"response": {
    "received": [
        {
            "items": [
                {

                    "classid": "356464564",

                },
                {
                    "classid": "456456456",

                },
                {
                    "classid": "356464564",

                },
                {
                    "classid": "721248158",

                }
            ]
            ,
            "time_created": 1440782791,
        },
        {
        "items": [
                {

                    "classid": "845362344",

                },
                {
                    "classid": "2543634754",

                },
                {
                    "classid": "2543634754",

                },
                {
                    "classid": "5967856788",

                }
            ]
            ,
            "time_created": 1440456791,
        }

}

}

这可以满足您的需求:

<?php

  $array = json_decode('{
      "response": {
          "received": [
              {
                  "items": [
                      {
                          "classid": "356464564"
                      },
                      {
                          "classid": "456456456"
                      },
                      {
                          "classid": "356464564"
                      },
                      {
                          "classid": "721248158"
                      }
                  ]
                  ,
                  "time_created": 1440782791
              }
          ]
      }
  }', true);

  $cleanArray = array();

  foreach($array['response']['received'][0]['items'] as $classid)
  {
    if(in_array($classid['classid'], $cleanArray))
      echo "Duplicate found: ".$classid['classid'].'<br>';
    else
      $cleanArray[] = $classid['classid'];  
  }

?>

试试这个,当找到匹配项时,它会使数组变小,从而使算法更有效

$arr = json_decode($json , true);
$items_array = array_column($arr['response']['items'], 'classid');

foreach ($items_array as $k => $val) {
    foreach ($items_array as $k2 => $val2) {
        if ($k2 != $k) {
            if ($val == $val2) {
                unset($items_array[$k]);
                if (isset($final[$val])) {
                    $final[$val]++;
                } else {
                    $final[$val] = 1;
                }
            }
        }
    }
}

var_dump($final); //will show you your duplicates

请向我们展示您尝试过的内容。请展示您尝试过但失败的内容。如果没有
true
标志,
$array
将被解析为
stdClass()
您也无法回显数组
$classid
,在提供答案之前,您需要推动OP以尝试显示代码,否则他们基本上是在利用您来完成工作。@AedixRhinedale,谢谢!我在回答中更正了它。@CodeGodie是对的,
$classid
将像
array(1){[classid]=>3564564}
一样输出。您应该将您的
foreach
改为
$key=>$value