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);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
array ( 0 => (object) array( 'Part ID' => '001', 'name' => 'Product Name', 'category' => 'Product Category', 'description' => 'Product Description', 'url' => 'www.123.com/x.jpg', ), 1 => (object) array( 'Part ID' => '002', 'name' => 'Product Name 2', 'category' => 'Product Category 2', 'description' => 'Product Description 2', ), 2 => (object) array( 'Part ID' => '003', 'name' => 'Product Name 3', 'category' => 'Product Category 3', 'description' => 'Product Description 3', ), 3 => (object) array( 'Part ID' => '004', 'name' => 'Product Name 4', 'category' => 'Product Category', 'description' => 'Product Description', 'feature_img_url' => array ( 0 => 'www.123.com/f1.jpg', 1 => 'www.123.com/f2.jpg', 2 => 'www.123.com/f3.jpg', ), ), )

preferences:
106.69 ms | 1412 KiB | 4 Q