After decoding a json string that represents a multidimensional object array, I am trying to map one of its objects to another of its objects based on matching id
values.
My input:
$data = array (
0 =>
(object) array(
'products' =>
array (
0 =>
(object) array(
'Part ID' => '001',
'name' => 'Product Name',
'category' => 'Product Category',
'description' => 'Product Description',
'id' => '111xyz',
),
1 =>
(object) array(
'Part ID' => '002',
'name' => 'Product Name 2',
'category' => 'Product Category 2',
'description' => 'Product Description 2',
'id' => '333xyz',
),
2 =>
(object) array(
'Part ID' => '003',
'name' => 'Product Name 3',
'category' => 'Product Category 3',
'description' => 'Product Description 3',
'id' => '444xyz',
),
3 =>
(object) array(
'Part ID' => '004',
'name' => 'Product Name 4',
'category' => 'Product Category',
'description' => 'Product Description',
'id' => '666xyz',
'features_img_id' =>
array (
0 => 'f1',
1 => 'f2',
2 => 'f3',
),
),
),
),
1 =>
(object) array(
'assets' =>
array (
0 =>
(object) array(
'File Name' => 'Some file name',
'url' => 'www.123.com/x.jpg',
'id' => '111xyz',
),
1 =>
(object) array(
'File Name' => 'Feature 1 file',
'url' => 'www.123.com/f1.jpg',
'id' => 'f1',
),
2 =>
(object) array(
'File Name' => 'Feature 2 file',
'url' => 'www.123.com/f2.jpg',
'id' => 'f2',
),
3 =>
(object) array(
'File Name' => 'Feature 2 file',
'url' => 'www.123.com/f3.jpg',
'id' => 'f3',
),
),
),
);
Desired result:
Array
(
[0] => stdClass Object
(
[Part ID] => 001
[name] => Product Name 1
[category] => Product Category
[description] => Product Description
[url] => www.123.com/x.jpg
)
[1] => stdClass Object
(
[Part ID] => 002
[name] => Product Name 2
[category] => Product Category 2
[description] => Product Description 2
)
[2] => stdClass Object
(
[Part ID] => 003
[name] => Product Name 3
[category] => Product Category 3
[description] => Product Description 3
)
[3] => stdClass Object
(
[Part ID] => 004
[name] => Product Name 4
[category] => Product Category 4
[description] => Product Description 4
[feature_img-url] => Array
(
[0] => www.123.com/f1.jpg
[1] => www.123.com/f3.jpg
[2] => www.123.com/f3.jpg
)
)
)
Below is what I tried, but it is only producing the one array.
$data = json_decode($dData, false);
$products = $data[0]->products;
$assets = $data[1]->assets;
$newArr;
foreach ($products as $key => $value) {
$newArr = $value->features_img;
}
$processedProducts = $products;
foreach ($processedProducts as $key => $product) {
foreach ($assets as $asset) {
if ($asset->id == $product->id) {
$product->url = $asset->url;
} elseif ($asset->id == in_array($asset->id, $newArr)) {
$product->features_image_url = $asset->url;
} else {
//code
}
}
unset($product->id);
}
print_r($processedProducts);
I don't know how to target the 'feature_img_id' array and match it with the assets ids and add it to the $processedProducts
.
2条答案
按热度按时间m4pnthwp1#
You can do it this way:
Will lead to the resulting array below:
ivqmmu1c2#
Code: ( Demo )