你能帮我生成护理产品吗? 它类似于this stackoverflow。我想生成输入,所以我需要保留ID。
示例:
我的输入数据:
result
结果我期待:
[
1 => [
id => 1,
name => "Color",
options => [
5 => [
id => 5,
name => "Red"
],
6 => [
id => 6,
name => "Blue"
]
]
],
2 => [
id => 2,
name => "Size",
options => [
7 => [
id => 7,
name => "S"
],
8 => [
id => 8,
name => "M"
]
]
],
// etc
]
我需要任意数量的属性/选项的通用函数..
答案 0 :(得分:0)
嵌套循环man,数组1的每个条目都必须与array2的每个条目链接。
$ finalArray = array();
foreach (array1 as $key1 as $value1){
foreach (array2 as $key2 as$value2){
echo $value1 . " - " .$value2."<br/>";
$finalArray[$key1.'-'.$key2] = $value1 ." - ".$value2;
}
}
finalArray将满足您的需求。
答案 1 :(得分:0)
这实际上是现在的工作代码,但不知道效率有多高。
// filter out properties without options
$withOptions = array_filter($properties, function($property) {
return count($property['options']) > 0;
});
$result = [];
$skipFirst = true;
foreach ($withOptions as $property) {
if ($skipFirst) {
foreach (reset($withOptions)['options'] as $id => $option) {
$result[$id] = $option['name'];
}
$skipFirst = false;
continue;
}
foreach ($result as $code => $variant) {
foreach ($property['options'] as $id => $option) {
$new = $code . "-" . $id;
$result[$new] = $variant . " / " . $option['name'];
unset($result[$code]);
}
}
}