0

I have JSON array values as given below,

{
  "test_details": [
    {
      "test_id": "001",
      "test_name": "Blood Test",
      "group_name": "Group 1",
      "package_details": [
        {
          "package_name": "master health checkup",
          "package_id": "1"
        },
        {
          "package_name": "special camp package",
          "package_id": "2"
        }
      ]
    },
    {
      "test_id": "002",
      "test_name": "haemoglobin",
      "group_name": "Group 2",
      "package_details": [
        {
          "package_name": "master health checkup",
          "package_id": "1"
        }
      ]
    },
    {
      "test_id": "003",
      "test_name": "creatine test",
      "group_name": "Group 2",
      "package_name": null
    }
  ]
}

In the above JSON, I want to group the values by the key package_name, which means I want to order the test list against the package name.

I have tried several array group by functions but for a dynamic key package_name, it is not working.

My final output JSON would be:

{
  "test_details": {
    "master health checkup": [
      {
        "test_id": "001",
        "test_name": "Blood Test",
        "group_name": "Group 1",
        "package_details": [
          {
            "package_name": "master health checkup",
            "package_id": "1"
          },
          {
            "package_name": "special camp package",
            "package_id": "2"
          }
        ]
      },
      {
        "test_id": "002",
        "test_name": "haemoglobin",
        "group_name": "Group 2",
        "package_details": [
          {
            "package_name": "master health checkup",
            "package_id": "1"
          }
        ]
      }
    ],
    "special camp package": [
      {
        "test_id": "001",
        "test_name": "Blood Test",
        "group_name": "Group 1",
        "package_details": [
          {
            "package_name": "master health checkup",
            "package_id": "1"
          },
          {
            "package_name": "special camp package",
            "package_id": "2"
          }
        ]
      }
    ]
  }
}

here you can see the master health checkup have 2 tests and under and same as special camp package have 1 test. how to achieve this? how to apply array group by when the array key is dynamic?

2
  • Could you put an example of your desire output there? Commented Oct 25, 2017 at 15:36
  • 1
    @EmilioGort it was already there, but another editor deleted it for some reason. I put it back. Commented Oct 25, 2017 at 15:41

1 Answer 1

0

After decoding the JSON, iterate the test_details. If a test has a package_details property, then iterate that and assign the test to your result array using each package name as the key.

$data = json_decode($json);

foreach ($data->test_details as $test) {
    if (isset($test->package_details)) {
        foreach ($test->package_details as $package) {
            $result['test_details'][$package->package_name][] = $test;
        }
    }
}

echo json_encode($result);
Sign up to request clarification or add additional context in comments.

Comments

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.