4 回答

TA貢獻1963條經驗 獲得超6個贊
$array = [
{
"pid": "1",
"pname": "Burger",
"price": "20",
"qnty": "1",
"pimg": "1.jpg"
},
{
"pid": "2",
"pname": "Cheese burger",
"price": "30",
"qnty": "1",
"pimg": "2.jpg"
}
]
你可以使用isset功能。
$key = 1;
$count =0 ;
foreach($array as $a) {
if(isset(array_search($key,$a))){
//your logic to execute if the $key is there in the array
$count ++;
}
}
if($count==0){
//add to cart logic
}

TA貢獻1829條經驗 獲得超7個贊
您需要使用循環迭代數組,然后根據數組的任何索引檢查值
foreach ($items as $item){
if($item->pid == 1) {
// do whatever you want to do
}
}

TA貢獻1827條經驗 獲得超8個贊
您可以利用isset()和!empty()
foreach($lists as $list) {
if(isset($list['pid']) && (!empty($list['pid']))) { // this will check if a key exists. If yes, then check for it's value. If value is there, then true
// pid exists and has a value
} else {
}
}
- 4 回答
- 0 關注
- 200 瀏覽
添加回答
舉報