$list = [
{
"id":67,
"text":"Brands1",
"class_3_main":[
{
"originalId":12,
"name":"USA",
"Id":"1",
"mainSupplierIds":"23, 24, 32, 21, 39, 84, 23"
},
{
"tag_id":6825,
"name":"UK",
"Id":"2307",
"mainSupplierIds":"47, 11"
}
],
"class_3_suppliers":[
{
"originalId":6927,
"name":"USA Suppliers",
"supplierIds":''
},
{
"tag_id":6928,
"name":"UK Suppliers",
"supplierIds":
}
],
}
]
I want the column mainSupplierIds
from the array class_3_main
to be linked to supplierIds
for class_3_suppliers
array.
At the end I expect the output to be:
"class_3_suppliers":[
{
"originalId":6927,
"name":"USA Suppliers",
"supplierIds":'23, 24, 32, 21, 39, 84, 23'
},
{
"tag_id":6928,
"name":"UK Suppliers",
"supplierIds":'47, 11'
}
],
I was using the below code to achieve this:
$result = array_map(create_function('$arr', 'return $arr["mainSupplierIds"];'), $class_3_main);
$class_3_suppliers['supplierIds'] = $result;
But the result combines the both supplierIds and the resukt returned is
"class_3_suppliers":[
{
"originalId":6927,
"name":"USA Suppliers",
"supplierIds":[
0: '23, 24, 32, 21, 39, 84, 23',
1: '47,11'
]
},
{
"tag_id":6928,
"name":"UK Suppliers",
"supplierIds":[
0: '23, 24, 32, 21, 39, 84, 23',
1: '47,11'
]
} ],
If this is supposed to be a PHP object with these values and they will always have the same names for the properties with the ID's and the same amount in both class_3_main and class_3_suppliers then this can work.
$list = [
"id"=>67,
"text"=>"Brands1",
"class_3_main"=>[[
"originalId"=>12,
"name"=>"USA",
"Id"=>"1",
"mainSupplierIds"=>"23, 24, 32, 21, 39, 84, 23"
],
[
"tag_id"=>6825,
"name"=>"UK",
"Id"=>"2307",
"mainSupplierIds"=>"47, 11"
]
],
"class_3_suppliers"=>[
[
"originalId"=>6927,
"name"=>"USA Suppliers",
"supplierIds"=>""
],
[
"tag_id"=>6928,
"name"=>"UK Suppliers",
"supplierIds"=>""
]
]
];
$class3Mains = $list["class_3_main"];
$class3Suppliers = $list["class_3_suppliers"];
$index = 0;
foreach($class3Mains as $class3Main) {
$class3Suppliers[$index]["supplierIds"] = $class3Main["mainSupplierIds"];
$index++;
}
var_dump($class3Suppliers);
You take out the array from class_3_main and class_3_suppliers and loop them and copy the value from one property to the other. The script I have added can be tested here: http://www.writephponline.com/