php - 根据匹配值从两个数组创建新数组
问题描述
尝试根据匹配值从两个对象数组创建一个新数组,但不知道如何实现这一点。
$dData = '[
{
"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"
}]
}
]';
新数组的期望结果
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
)
)
)
以下是我尝试过的,但它只产生一个数组
$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);
如果我在这里完全是新手,我很抱歉。不知道如何定位“feature_img_id”数组并将其与资产 ID 匹配并将其添加到 $processedProducts。请查看获得该想法所需的结果。
解决方案
你可以这样做:
$dData = '[
{
"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"
}]
}
]';
$data = json_decode($dData, false);
$products = $data[0]->products;
$assets = $data[1]->assets;
$processedProducts = $products;
foreach($processedProducts as $key => $product) {
foreach($assets as $asset) {
if ($asset->id == $product->id) {
$product->url = $asset->url;
}
if (isset($product->features_img_id) && in_array($asset->id, $product->features_img_id)) {
$product->feature_image_url[] = $asset->url;
}
}
unset($product->id);
unset($product->features_img_id);
}
将导致下面的结果数组:
Array
(
[0] => stdClass Object
(
[Part ID] => 001
[name] => Product Name
[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
[description] => Product Description
[feature_image_url] => Array
(
[0] => www.123.com/f1.jpg
[1] => www.123.com/f2.jpg
[2] => www.123.com/f3.jpg
)
)
)
推荐阅读
- php - 如何在我的网页中集成可以由其他人下载的 pdf 文件?
- bert-language-model - WordPiece Embedding 如何在 BERT 输出上工作
- sql - 从表中选择 2 个最近的日期 - SQL
- typescript - 函数返回类型参数依赖 Typescript
- sorting - 矩阵中不同位置的最大排序子序列
- python - 熊猫计算行中字符串并发的最快方法(句子中的单词)
- hadoop - Hive:有什么方法可以提前预测 Parquet 存储表的大小?
- php - oci_fetch_array 未返回预期值
- w3c - 如何解决此错误 - 信息:检测到的方言:W3C
- php - 雄辩的保存数据而不进行强制转换以避免绕过“有效负载无效”。