title: Group an array with dynamic key values?
body: <p>I  have JSON array values as given below,</p>
  
  <pre><code>{
    "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
      }
    ]
  }
  </code></pre>
  
  <p>In the above JSON, I want to group the values by the key <code>package_name</code>, which means I want to order the test list against the package name.</p>
  
  <p>I have tried several array group by functions but for a dynamic key <code>package_name</code>, it is not working.</p>
  
  <p>My final output JSON would be:</p>
  
  <pre><code>{
    "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"
            }
          ]
        }
      ]
    }
  }
  </code></pre>
  
  <p>here you can see the <strong>master health checkup</strong> have 2 tests and under and same as <strong>special camp package</strong> have 1 test. how to achieve this? how to apply array group by when the array key is dynamic? </p>