如果条件为假,如何使用新项目启动foreach
。我有这样的代码:
foreach($a_auctions as $auction){
# get the winner
if($auction['for_canada'] == 1 && $isFromCanada){
// another solution to start the foreach with new item (break, exit) ?
$this->a_returnData[] = array(
'winner' => 'pseudo',
'title' => 'title',
'price' => 200,
);
}else{
$this->a_returnData[] = array(
'winner' => 'pseudo',
'title' => 'title',
'price' => 200,
);
}
}
我想在if中从foreach if条件不为真的新项开始,这样做是为了避免重复代码。我添加了一条评论。
发布于 2017-05-29 19:19:31
我相信您正在寻找reset()命令!这会将数组指针倒回到开头,请查看手册!(链接如下)
http://php.net/manual/en/function.reset.php
如果要将另一个元素添加到数组的开头,请使用array_unshift():
https://stackoverflow.com/questions/44240341
复制相似问题