1 回答
TA贡献2016条经验 获得超9个赞
您需要将 post 数组转换为此结构以便于处理
$posts = [
'Model' => ['Mustang', 'Camero'],
'Color' => ['Red']
];
然后使用 foreach 过滤器:
$carJson = '{
"Inventory": [{
"id": "1",
"StockNumber": "1000",
"Make": "Ford",
"Model": "Mustang",
"Trim": "GT",
"Year": "2011",
"Color": "Red",
"Cylinders": "8",
"Transmission": "Manual"
}, {
"id": "2",
"StockNumber": "1001",
"Make": "Ford",
"Model": "Mustang",
"Trim": "GT",
"Year": "2012",
"Color": "Yellow",
"Cylinders": "8",
"Transmission": "Automatic"
}, {
"id": "3",
"StockNumber": "1002",
"Make": "Chevy",
"Model": "Camaro",
"Trim": "ZL1",
"Year": "2020",
"Color": "Red",
"Cylinders": "8",
"Transmission": "Manual"
},
{
"id": "4",
"StockNumber": "1005",
"Make": "Chevy",
"Model": "VW",
"Trim": "ZL1",
"Year": "2020",
"Color": "Red",
"Cylinders": "8",
"Transmission": "Manual"
}]
}';
$cars = json_decode($carJson, true)['Inventory'];
$posts = [
'Model' => ['Mustang', 'Camero'],
'Color' => ['Red']
];
$result = [];
foreach ($posts as $postKey => $postValue) {
foreach ($cars as $k => $car) {
if (array_key_exists($postKey, $car) && in_array($car[$postKey], $postValue)) {
$result[$k] = $car;
} else {
unset($cars[$k]);
unset($result[$k]);
}
}
}
print_r($result);
- 1 回答
- 0 关注
- 137 浏览
添加回答
举报