<?php $arr = Array ( 0 => Array ( 'template_id' => 1, 'template_field_id' => 20 ), 1 => Array ( 'template_id' => 1, 'template_field_id' => 21 ), 2 => Array ( 'template_id' => 2, 'template_field_id' => 22 ), 3 => Array ( 'template_id' => 2, 'template_field_id' => 23 ) ); $resultArr = array(); foreach($arr as $data){ $index = array_search($data['template_id'], array_column($resultArr, 'id')); if($index !== false){ if(is_array($resultArr[$index]["template_fields"])) array_push($resultArr[$index]["template_fields"],array("id"=>$data["template_field_id"])); else $resultArr[$index]["template_fields"] = array(array("id"=>$data["template_field_id"])); }else{ $newData = array("id"=>$data['template_id'],"template_fields"=>array(array("id"=>$data["template_field_id"]))); array_push($resultArr,$newData); } } print_r($resultArr);
You have javascript disabled. You will not be able to edit any code.