If there is multiple persons with the same name you can't use array_combine.
This method uses array_intersect to find the codes that match the name and create a subarray on each name whith all the codes.
$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);
output:
array(3) {
["John"]=>
array(2) {
[0]=>
string(3) "006"
[3]=>
string(3) "010"
}
["James"]=>
array(1) {
[1]=>
string(3) "007"
}
["Jone"]=>
array(1) {
[2]=>
string(3) "008"
}
}
https://3v4l.org/Tj4Dd