1

I had a Response coming like a JSON array like below and I need to sort out the JSON containing the same roleId and TenantId and send them with all the features in the same array.

[
    {
        "roleId": 1,
        "role": "admin",
        "featureId": 1,
        "feature": "Dashboard",
        "tenantId": 1,
        "tenant": "Admin"
    },
    {
        "roleId": 1,
        "role": "admin",
        "featureId": 2,
        "feature": "Overview",
        "tenantId": 1,
        "tenant": "Admin"
    },
    {
        "roleId": 1,
        "role": "admin",
        "featureId": 3,
        "feature": "Devices",
        "tenantId": 1,
        "tenant": "Admin"
    },
    {
        "roleId": 1,
        "role": "admin",
        "featureId": 4,
        "feature": "Map View",
        "tenantId": 1,
        "tenant": "Admin"
    }
]

I need to customize it and return as a response like below grouping by tenantid and role in a sub json

{
    "roleId": 1,
    "role": "admin",
    "features": [
        {
            "featureId": 1,
            "feature": "Dashboard"
        },
        {
            "featureId": 2,
            "feature": "Overview",
        },
        {
            "featureId": 4,
            "feature": "Map View",
        },
        {
            "featureId": 3,
            "feature": "Devices",
        }
    ],
    "tenantId": 1,
    "tenant": "Admin"
},
{
    "roleId": 2,
    "role": "monitor",
    "features": [
        {
            "featureId": 1,
            "feature": "Dashboard"
        },
        {
            "featureId": 2,
            "feature": "Overview",
        },
        {
            "featureId": 4,
            "feature": "Map View",
        },
        {
            "featureId": 3,
            "feature": "Devices",
        }
    ],
    "tenantId": 1,
    "tenant": "Admin"
}

Expected UI should be something like below after integration

UI Image

Please help me out in this

Thanks in advance

5
  • 1
    You can not have same index in your desired output array. they will get overWritten [feature,featureId ... etc]. See here:- 3v4l.org/Ze2SY Commented May 8, 2019 at 7:03
  • Its not possible to achieve the response in below Commented May 8, 2019 at 7:04
  • That can't be parsed back to an array later, you know that? An array can only have one key with the same name in the same subarray Commented May 8, 2019 at 7:04
  • I have changed the required response, can you please look @AlivetoDie Commented May 8, 2019 at 9:53
  • Tqs for the two way array binding solution :) Commented May 14, 2019 at 8:56

1 Answer 1

1

1.You need to decode your json data using json_decode()

2.Then you need to use foreach() to loop this data

3.Now create a new array and assign values to this array inside loop by making roleId as array keys[so that same roleId values will goes to same child-array automatically]

4.Now do array_values() to re-index the final array

5.Encode this using json_encode() to get desired format.

Code:-

$array = json_decode( $json , true);

$final_array = array();

foreach($array as $arr){
    $final_array[$arr['roleId']]['roleId'] = $arr['roleId'];
    $final_array[$arr['roleId']]['role'] = $arr['role'];
    $final_array[$arr['roleId']]['feature'][] = array('featureId'=>$arr['featureId'],'feature'=>$arr['feature']);
}

$final_array = array_values($final_array);
echo json_encode($final_array);

Output:-https://3v4l.org/CuCI2

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.