简体   繁体   English

合并键在同一数组中匹配的关联数组值

[英]Merging associative array values where keys match within the same array

I am generating an associative array that I want to use to generate a JSON object but have a problem of duplicate keys with different values. 我正在生成一个关联数组,该数组要用于生成JSON对象,但是存在重复键值不同的问题。 I want the where the keys are duplicate, the values of are merged. 我想要键重复的位置,合并值。 I have checked online but all the solutions reference 2 different arrays not the same array. 我已经在线检查过,但是所有解决方案都引用2个不同的数组,而不是同一数组。

The JSON Object is: JSON对象是:

{
  "trailervideos": [
    {
      "category": "Video Games",
      "videos": {
        "description": "Trailer of the game Dark Souls II",
        "title": "Dark Souls 2 Announcement Trailer"
      }
    },
    {
      "category": "Video Games",
      "videos": {
        "description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
        "title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
      }
    },
    {
      "category": "Video Games",
      "videos": {
        "description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
        "title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
      }
    },
    {
      "category": "Entertainment",
      "videos": {
        "description": "",
        "title": "intro"
      }
    }
  ]
}

What I want to achieve is all the values of repeated key "Video Games" combined so that I can generate a JSON object like: 我想要实现的是将重复键“视频游戏”的所有值组合在一起,以便生成如下的JSON对象:

{"trailervideos":[{"category":"Video Games","videos":[{"description":"Trailer of the game Dark Souls II","title":"Dark Souls 2 Announcement Trailer"},{"description":"Trailer of the DLC Scholar of the First Sin for the game Dark Souls II","title":"Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"},{"description":"Trailer of the DLC Ashes of Ariendel for the game Dark Souls III","title":"Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"}],{"category":"Entertainment","videos":{"description":"","title":"intro"}}]}

There is no 'JSON-object'. 没有“ JSON对象”。 JSON is a string reprentation a J avas S cript O bject N otation you can build a PHP object-array-structure from, using json_decode . JSON是一个字符串reprentation为J AVAS 小号 CRIPTöbjectÑ浮选可以构建PHP从对象的阵列结构,采用json_decode To get a JSON string from PHP variables, the function json_encode is used. 要从PHP变量获取JSON字符串,请使用函数json_encode

The easiest way is to iterate over the trailervideos building a new associative array since there can only be unique keys. 最简单的方法是遍历trailervideos构建新的关联数组,因为只能有唯一的键。 We can eleminate the key names by the function array_values later to prevent json_encode from building an object instead of an array because associative arrays do not exist in JavaScript. 稍后我们可以通过array_values函数消除键名,以防止json_encode构建对象而不是数组,因为JavaScript中不存在关联数组。

This version does process all categories, "Video Games" as well as "Entertainment" and, if available, even more. 此版本可处理所有类别的内容,包括"Video Games""Entertainment"以及更多内容。

$a = [];

foreach (($o = json_decode($json))->trailervideos as $v)
{
  isset($a[$v->category]) || $a[$v->category] = new stdClass();
  $a[$v->category]->category = $v->category;
  $a[$v->category]->videos[] = $v->videos;
}

$o->trailervideos = array_values($a);

var_dump(json_encode($o));

The (formatted) JSON result looks like that: (格式化的)JSON结果如下所示:

{
  "trailervideos": [
    {
      "category": "Video Games",
      "videos": [
        {
          "description": "Trailer of the game Dark Souls II",
          "title": "Dark Souls 2 Announcement Trailer"
        },
        {
          "description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
          "title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
        },
        {
          "description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
          "title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
        }
      ]
    },
    {
      "category": "Entertainment",
      "videos": [
        {
          "description": "",
          "title": "intro"
        }
      ]
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM