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 關注
- 133 瀏覽
添加回答
舉報