1

I have a json response like below:

$response ='[
    {
        "userSummaries": [
            {
                "id": "9910",
                "status": "Active",
                "name": "Jhon"
            }
        ]
    },
    {
        "userSummaries": [
            {
                "id": "8754",
                "status": "Active",
                "name": "Jane"
            }
        ]
    }
]';

and I would like to group this by userSummaries with this php code:

$myArr = json_decode($response, true);  

    $result_arr = [];
    
    array_walk($myArr,function($v,$k) use (&$result_arr){
       $result_arr[key($v)] = $v[key($v)]; 
    });
    
   
echo json_encode($result_arr);

and the response only return one data:

{"userSummaries":[{"id":"8754","status":"Active","name":"Jane"}]}

Is it possible to get the output response like this?:

{"userSummaries":[{"id":"9910","status":"Active","name":"Jhon"}, {"id":"8754","status":"Active","name":"Jane"}, ]}

Tried over the net but I did not found the solutions

here my script for this: https://3v4l.org/tVkK5

also tried this:

$class_array = array();
foreach ($myArr as $sa) {
    $class_array[$sa['userSummaries']][] = array('name' => $sa['name']);
}

but return:

Notice: Undefined index: name in /in/hvSFC on line 28

Warning: Illegal offset type in /in/hvSFC on line 28

Notice: Undefined index: name in /in/hvSFC on line 28

Warning: Illegal offset type in /in/hvSFC on line 28
[]

need help

1 Answer 1

2

You were close. You just needed to reference the key and first of userSummaries in each loop, instead of working with the whole...

$myArr = json_decode($response, true);
$result_arr = ["userSummaries"=>[]];
foreach($myArr as $user) {
    $result_arr["userSummaries"][] = $user['userSummaries'][0];
}
echo json_encode($result_arr);

Results in:

{"userSummaries":[
    {"id":"9910","status":"Active","name":"Jhon"},
    {"id":"8754","status":"Active","name":"Jane"}
]}

If you foresee that userSummaries in each will have multiple users themselves... then this would work:

$response ='[
    {
        "userSummaries": [
            {
                "id": "9910",
                "status": "Active",
                "name": "Jhon"
            }
        ]
    },
    {
        "userSummaries": [
            {
                "id": "8754",
                "status": "Active",
                "name": "Jane"
            },
            {
                "id": "5421",
                "status": "Active",
                "name": "Bob"
            }
        ]
    }
]';

$myArr = json_decode($response, true);
$result_arr = ["userSummaries"=>[]];
foreach($myArr as $usergroup) {
    foreach($usergroup['userSummaries'] as $user) {
        $result_arr["userSummaries"][] = $user;
    }
}
echo json_encode($result_arr);

Results in:

{"userSummaries":[
    {"id":"9910","status":"Active","name":"Jhon"},
    {"id":"8754","status":"Active","name":"Jane"},
    {"id":"5421","status":"Active","name":"Bob"}
]}
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.