1

I have this output results from database:

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
        )

)

now for json ouput:

    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);

output is:

[
   {
      "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"
         }
      ]
   }
]

But In Action I need to output grouped by attribute_group and listed in children like this:

[
   {
      "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"
         }
      ]
   }
]

how do can i create this json output?!

1 Answer 1

2

Instead of creating an array $json with an element for each attribute, you should gather each attribute directly by attribute_group_id.

In order to do that, the idea is to use the attribute_group_id as the key of your $json array ($json[$result->attribute_group_id]). If an entry already exists for $json[$result->attribute_group_id]['children'] then you just have to had the current children to this item. If not, you create the entry for the current attribute group ID with his information (id,text,children).

Finally you can return the $json without the key that we used for grouping the attributes using array_values (returns the values of an array without keys).

foreach ($results as $result) {  
    if(isset($json[$result->attribute_group_id]['children'])){
        $json[$result->attribute_group_id]['children'][] = array(
            'id' => $result->attribute_id,
            'text' => $result->name,
        );
    }
    else {
        $json[$result->attribute_group_id] = array(
            'id' => $result->attribute_group_id,
            'text' => $result->attribute_group,
            'children' => [array(
                'id' => $result->attribute_id,
                'text' => $result->name,
            )]
        );
    }
}

return json_encode(array_values($json));

Result :

[
  {
    "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"
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

1 Comment

adding an explanation why the problem is occurring and how your answer solves it is always a good idea for a quality answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.