下面的foreach
循环应该获取Sort
的每个值并将其附加到$cartesian[]
。当我执行代码时,它只使用salesrank
和titlerank
。
当我在没有foreach($allOptions['Sort'][$cartesian['SearchIndex']] as $possible)
的情况下运行主循环时,结果与预期一致。
当我添加foreach($allOptions['Sort'][$cartesian['SearchIndex']] as $possible)
时,它只会添加salesrank
和titlerank
。
我做错了什么?
$allOptions = array(
"Sort" => array(
"SportingGoods" => array("-price", "inverseprice", "launch-date", "price", "pricerank", "relevance-fs-rank", "relevancerank", "reviewrank_authority", "sale-flag", "salesrank"),
"Tools" => array("-price", "-titlerank", "pmrank", "price", "salesrank", "titlerank"),
"Appliances" => array("-price", "pmrank", "price", "relevancerank", "reviewrank", "reviewrank_authority", "salesrank"),
"Electronics" => array("-price", "pmrank", "price", "reviewrank", "salesrank", "titlerank"),
"MusicalInstruments" => array("-launch-date", "-price", "pmrank", "price", "sale-flag", "salesrank"),
"PetSupplies" => array("-price", "-titlerank", "price", "relevance", "relevancerank", "reviewrank", "reviewrank_authority", "salesrank", "titlerank"),
"OutdoorLiving" => array("-price", "-titlerank", "price", "psrank", "salesrank", "titlerank"),
"Industrial" => array("-price", "-titlerank", "pmrank", "price", "salesrank", "titlerank"),
"Automotive" => array("-price", "-titlerank", "price", "relevancerank", "salesrank", "titlerank"),
"HomeGarden" => array("-price", "-titlerank", "price", "salesrank", "titlerank")
$finalOptions = $allOptions;
unset($finalOptions['BrowseNode']);
unset($finalOptions['Sort']);
$cartesian_product = cartesian($finalOptions);
foreach($cartesian_product as $cartesian){
//add each browse node as is corresponds to the SearchIndex
foreach($allOptions['BrowseNode'][$cartesian['SearchIndex']] as $possible){
$cartesian['BrowseNode'] = $possible;
//add each sort option as it corresponds to the SearchIndex
foreach($allOptions['Sort'][$cartesian['SearchIndex']] as $possible){
$cartesian['Sort'] = $possible;
}
}
$this->itemSearch($cartesian);
sleep(1);
}
答案 0 :(得分:1)
你被覆盖,而不是追加。您需要使用[]
将元素推送到数组上。
foreach($cartesian_product as $cartesian){
$cartesian['BrowseNode'] = array();
$cartesian['Sort'] = array();
//add each browse node as is corresponds to the SearchIndex
foreach($allOptions['BrowseNode'][$cartesian['SearchIndex']] as $possible){
$cartesian['BrowseNode'][] = $possible;
//add each sort option as it corresponds to the SearchIndex
foreach($allOptions['Sort'][$cartesian['SearchIndex']] as $possible){
$cartesian['Sort'][] = $possible;
}
}
$this->itemSearch($cartesian);
sleep(1);
}
答案 1 :(得分:1)
看看这段代码(我删除了与讨论无关的部分):
foreach($allOptions['BrowseNode'][$cartesian['SearchIndex']] as $possible){
$cartesian['BrowseNode'] = $possible;
...
}
当然它不记得数组的所有值。您可以将它们一次又一次地存储在一个地方。每次迭代都会覆盖上一次迭代设置的值,循环完成后只保留最后一个值。
内循环也是如此:
foreach($allOptions['Sort'][$cartesian['SearchIndex']] as $possible){
$cartesian['Sort'] = $possible;
}
顺便说一下,没有理由把这个循环放在另一个循环中。它不依赖于$allOptions['BrowseNode']
上的循环上更改的任何值。更重要的是,因为两个循环使用相同的变量$possible
作为迭代器,所以在某些情况下可能会发生意外情况。
从问题中不清楚预期的结果是什么。假设您要存储整个数组(f {。$allOptions['Sort']['Tools']
在$cartesian['Sort']
,那么您只需一次分配整个数组:
foreach ($cartesian_product as $cartesian) {
// Extract common expression into a variable;
// this speeds the code up and makes it more readable
$index = $cartesian['SearchIndex'];
// Add each browse node as is corresponds to the SearchIndex
$cartesian['BrowseNode'] = $allOptions['BrowseNode'][$index];
// Add each sort option as it corresponds to the SearchIndex
$cartesian['Sort'] = $allOptions['Sort'][$index];
$this->itemSearch($cartesian);
sleep(1);
}
上面的代码将$cartesian['BrowseNode']
和$cartesian['Sort']
的值替换为从$allOptions
中提取的值。如果$cartesian
已经包含上述排名的值且必须保留这些值(以及$allOptions
中添加的新值),请使用array_merge()
代替分配:
...
// Add each sort option as it corresponds to the SearchIndex
$cartesian['Sort'] = array_unique(array_merge(
$cartesian['Sort'], $allOptions['Sort'][$index]
));
...
如果需要,可以在合并数组上使用array_unique()
删除重复项($cartesian['Sort']
中已存在且已从$allOptions['Sort'][$index]
添加的选项)。
答案 2 :(得分:0)
答案很简单。我在错误的地方调用我的功能。正确的方法是:
foreach($cartesian_product as $cartesian){
//add each browse node as is corresponds to the SearchIndex
foreach($allOptions['BrowseNode'][$cartesian['SearchIndex']] as $possible){
$cartesian['BrowseNode'] = $possible;
//add each sort option as it corresponds to the SearchIndex
foreach($allOptions['Sort'][$cartesian['SearchIndex']] as $possible){
$cartesian['Sort'] = $possible;
$this->itemSearch($cartesian);
sleep(1);
}
}
}
这为我提供了数据集的每种可能组合。