2 回答
TA贡献1803条经验 获得超6个赞
也许这也可以帮助你:
$array1 =Array ('0' => Array('qty'=>10),'1'=>Array('qty'=>5),'2'=>Array('qty'=>1));
$array2 =Array ('0' => Array('qty'=>15),'1'=>Array('qty'=>5),'2'=>Array('qty'=>1));
$array3 =Array ('0' => Array('qty'=>9),'1'=>Array('qty'=>4),'2'=>Array('qty'=>0));
function handleBasket($items)
{
$quantities = array_column($items, 'qty');
foreach($quantities as $quantity)
{
if($quantity >10){
return 'The requested qty is not available';
}
if($quantity ===0){
return 'Some of products are out of stock';
}
}
return 'Your order has been successfully processed';
}
echo handleBasket($array1);
echo handleBasket($array2);
echo handleBasket($array3);
输出 :
Your order has been successfully processed
The requested qty is not available
Some of products are out of stock
TA贡献1946条经验 获得超4个赞
foreach ($array as $key => $item) {
if($item['qty'] !== 0) {
if($item['qty'] <= 10)
{
$it = 'Your order has been successfully processed';
}
else
{
$it = 'The requested qty is not available';
break;
}
}
else
{
$it = 'Some of products are out of stock';
break;
}
}
echo $it;
你可以简单地使用休息;如果顺序出现问题,请停止 foreach。
就像一个想法:您也可以使用布尔值,如果一切正常则处理订单,如果没有给出特定的错误消息。取决于你接下来的步骤。
- 2 回答
- 0 关注
- 96 浏览
添加回答
举报