3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = json_decode('[ { "products": [ { "Part ID" : "001", "name" : "Product Name", "category" : "Product Category", "description" : "Product Description", "id" : "111xyz" }, { "Part ID" : "002", "name" : "Product Name 2", "category" : "Product Category 2", "description" : "Product Description 2", "id" : "333xyz" }, { "Part ID" : "003", "name" : "Product Name 3", "category" : "Product Category 3", "description" : "Product Description 3", "id" : "444xyz" }, { "Part ID" : "004", "name" : "Product Name 4", "category" : "Product Category", "description" : "Product Description", "id" : "666xyz", "features_img_id":["f1","f2","f3"] }] }, { "assets": [ { "File Name" : "Some file name", "url" : "www.123.com/x.jpg", "id" : "111xyz" }, { "File Name" : "Feature 1 file", "url" : "www.123.com/f1.jpg", "id" : "f1" }, { "File Name" : "Feature 2 file", "url" : "www.123.com/f2.jpg", "id" : "f2" }, { "File Name" : "Feature 2 file", "url" : "www.123.com/f3.jpg", "id" : "f3" }] } ]'); $map = array_column($array[1]->assets, 'url', 'id'); foreach ($array[0]->products as $obj) { if (isset($map[$obj->id])) { $obj->url = $map[$obj->id]; } foreach ($obj->features_img_id ?? [] as $imgId) { if (isset($map[$imgId])) { $obj->feature_img_url[] = $map[$imgId]; } } unset($obj->id, $obj->features_img_id); } var_export($array[0]->products);

preferences:
43.38 ms | 1412 KiB | 5 Q