使用php计算多个数组json中的出现次数

时间:2017-08-03 16:10:42

标签: php arrays json

我有一个json返回下面的结构。 我需要计算item_name的出现次数并打印如下信息:

<li>nameX(locationX) = 3</li>
<li>nameY(locationY) = 1</li>

我尝试使用array_count_values,但它无法使用多个数组和特定项目来计算&#34; item_name&#34;,item_location与item_name相同,并且不需要计算,只取一次值。

[
{
    0 : {
        item: {
            item_basic: {
                item_country: {
                    other_item: not important
                    item_name: nameX
                    other_item01: not important
                    item_location: locationX
                    other_item02: not important
                }
            }
        }
    }
    1 : {
        item: {
            item_basic: {
                item_country: {
                    other_item: not important
                    item_name: nameX
                    other_item01: not important
                    item_location: locationX
                    other_item02: not important
                }
            }
        }
    }
    2 : {
        item: {
            item_basic: {
                item_country: {
                    other_item: not important
                    item_name: nameY
                    other_item01: not important
                    item_location: locationY
                    other_item02: not important
                }
            }
        }
    }
    3 : {
        item: {
            item_basic: {
                item_country: {
                    other_item: not important
                    item_name: nameX
                    other_item01: not important
                    item_location: locationX
                    other_item02: not important
                }
            }
        }
    }
}
]

1 个答案:

答案 0 :(得分:0)

您的解决方案是使用foreach循环从每个数组中收集item_name值并将值放在单独的数组中。然后在放入值的数组中使用array_count_values

这是一个有效的解决方案:

<?php

 $JSON = '{
  "0": {
    "item": {
      "item_basic": {
        "item_country": {
          "other_item": "not important",
          "item_name": "nameX",
          "other_item01": "not important",
          "item_location": "locationX",
          "other_item02": "not important"
        }
      }
    }
  },
  "1": {
    "item": {
      "item_basic": {
        "item_country": {
          "other_item": "not important",
          "item_name": "nameX",
          "other_item01": "not important",
          "item_location": "locationX",
          "other_item02": "not important"
        }
      }
    }
  },
  "2": {
    "item": {
      "item_basic": {
        "item_country": {
          "other_item": "not important",
          "item_name": "nameY",
          "other_item01": "not important",
          "item_location": "locationY",
          "other_item02": "not important"
        }
      }
    }
  },
  "3": {
    "item": {
      "item_basic": {
        "item_country": {
          "other_item": "not important",
          "item_name": "nameY",
          "other_item01": "not important",
          "item_location": "locationY",
          "other_item02": "not important"
        }
      }
    }
  }
}';

 $JSON = json_decode($JSON, TRUE);
 $item_names = array();
 foreach ($JSON as $item)
     {
     array_push($item_names, $item['item']['item_basic']['item_country']['item_name']);
     }

     $items_counted = array_count_values($item_names);



print_r($items_counted);

输出:

Array ( [nameX] => 2 [nameY] => 2 )

此外,array_count_values表示它仅适用于一个阵列:

  

array_count_values - 计算数组的所有值