php打印组中的json输出数据

php打印组中的json输出数据,php,arrays,json,Php,Arrays,Json,我从数据库中得到以下输出结果: Array ( [0] => stdClass Object ( [name] => attr one [attribute_id] => 1 [attribute_group] => group one [attribute_group_id] => 1 ) [1] => stdC

我从数据库中得到以下输出结果:

Array

(
    [0] => stdClass Object
        (
            [name] => attr one
            [attribute_id] => 1
            [attribute_group] => group one
            [attribute_group_id] => 1
        )

    [1] => stdClass Object
        (
            [name] => attr two
            [attribute_id] => 2
            [attribute_group] => group one
            [attribute_group_id] => 1
        )

    [2] => stdClass Object
        (
            [name] => attr three
            [attribute_id] => 3
            [attribute_group] => group one
            [attribute_group_id] => 1
        )

    [3] => stdClass Object
        (
            [name] => attr four
            [attribute_id] => 4
            [attribute_group] => group two
            [attribute_group_id] => 2
        )

)
现在,对于json输出:

    foreach ($results as $result) {
        $json[] = array(
            'id' => $result->attribute_group_id,
            'text' => $result->attribute_group,
            'children' => [array(
                'id' => $result->attribute_id,
                'text' => $result->name,
            )]
        );
    }
    return json_encode($json);
输出为:

[
   {
      "id":"1",
      "text":"group one",
      "children":[
         {
            "id":"1",
            "text":"attr one"
         }
      ]
   },
   {
      "id":"1",
      "text":"group one",
      "children":[
         {
            "id":"2",
            "text":"attr two"
         }
      ]
   },
   {
      "id":"1",
      "text":"group one",
      "children":[
         {
            "id":"3",
            "text":"attr three"
         }
      ]
   },
   {
      "id":"2",
      "text":"group two",
      "children":[
         {
            "id":"4",
            "text":"attr four"
         }
      ]
   }
]
但在实际操作中,我需要按
属性\u组
分组并在
子项
中列出如下输出:

[
   {
      "id":"1",
      "text":"group one",
      "children":[
         {
            "id":"1",
            "text":"attr one"
         },
            "id":"2",
            "text":"attr two"
         },
         {
            "id":"3",
            "text":"attr three"
         }
      ]
   },
   {
      "id":"2",
      "text":"group two",
      "children":[
         {
            "id":"4",
            "text":"attr four"
         }
      ]
   }
]

如何创建这个json输出

不应该为每个属性创建一个带有元素的数组$json,而应该通过属性组id直接收集每个属性

为了做到这一点,我们的想法是使用属性组id作为$json数组的键($json[$result->attribute\u group\u id])。如果$json[$result->attribute_group_id]['children']的条目已经存在,那么您只需将当前子项添加到此项即可。如果没有,则使用当前属性组ID的信息(ID、文本、子项)为其创建条目

最后,您可以返回$json而不使用我们用于使用array_值对属性进行分组的键(返回不带键的数组的值)

结果:

[
  {
    "id": "1",
    "text": "group one",
    "children": [
      {
        "id": "1",
        "text": "attr one"
      },
      {
        "id": "2",
        "text": "attr two"
      },
      {
        "id": "3",
        "text": "attr three"
      }
    ]
  },
  {
    "id": "2",
    "text": "group two",
    "children": [
      {
        "id": "4",
        "text": "attr four"
      }
    ]
  }
]

添加问题发生的原因以及您的答案如何解决的解释,对于高质量的答案来说总是一个好主意。
[
  {
    "id": "1",
    "text": "group one",
    "children": [
      {
        "id": "1",
        "text": "attr one"
      },
      {
        "id": "2",
        "text": "attr two"
      },
      {
        "id": "3",
        "text": "attr three"
      }
    ]
  },
  {
    "id": "2",
    "text": "group two",
    "children": [
      {
        "id": "4",
        "text": "attr four"
      }
    ]
  }
]