Store object into array and group all array with the same value in PHP Store object into array and group all array with the same value in PHP laravel laravel

Store object into array and group all array with the same value in PHP


Collection function might help you find your solution.

$json = '{"data":{"id":2,"title":"second evaluation form","emp_position":"System Architecture","rating":5,"segments":[{"segment_name":"Job Role ","question":"How old are you?"},{"segment_name":"360 Segments","question":"What is your food?"},{"segment_name":"360 Segments","question":"sample question"}]}}';    $array = json_decode($json, true);    $coll = collect($array['data']['segments']);    $coll = $coll->groupBy('segment_name');    dump($coll);

Hope this helps you.Let me know if any problem


Do it like below:-

<?php$json = '{        "data": {            "id": 2,            "title": "second evaluation form",            "emp_position": "System Architecture",            "rating": 5,            "segments": [                {                    "segment_name": "Job Role ",                    "id": 4,                    "question": "How old are you?"                },                {                    "segment_name": "360 Segments",                    "id": 1,                    "question": "What is your food?"                },                {                    "segment_name": "360 Segments",                    "id": 2,                    "question": "sample question"                }            ]        }    }';$query = json_decode($json,true);$segment_array = [];foreach($query['data']['segments'] as $arr){  $segment_array[$arr['segment_name']]['segment_name'] = $arr['segment_name'];  $segment_array[$arr['segment_name']]['question_collection'][] = ['id'=>$arr['id'],'question'=>$arr['question']] ;}$query['data']['segments'] = array_values($segment_array);echo json_encode($query,JSON_PRETTY_PRINT);

OUTPUT:- https://eval.in/902194


Try this solution, You can loop by array and group all keys

$json = '{        "data": {            "id": 2,            "title": "second evaluation form",            "emp_position": "System Architecture",            "rating": 5,            "segments": [                {                    "segment_name": "Job Role ",                    "question": "How old are you?"                },                {                    "segment_name": "360 Segments",                    "question": "What is your food?"                },                {                    "segment_name": "360 Segments",                    "question": "sample question"                }            ]        }    }';$data = json_decode($json,true);$segments = $data['data']['segments'];$new_segemnts = array();foreach($segments as $segemnt){    $key = $segemnt['segment_name'];    $new_segemnts[$key]['segment_name']=$segemnt['segment_name'];    $new_segemnts[$key]['question_collection'][]=array("question"=>$segemnt['question']);}$data['data']['segments'] = array_values($new_segemnts);echo json_encode($data,JSON_PRETTY_PRINT);

DEMO