$arr = json_decode($str, true); $names = array_column($arr, "name"); $codes = array_column($arr, "code"); foreach(array_unique($names) as $name){ $new[$name] = array_intersect_key($codes, array_intersect($names, [$name])); } var_dump($new);