Array ( [0] => Array ( [0] => Array ( [subject] => Computer [price] => 33.00 [quantity] => 1 ) ) )
我有一个如上所示的数组,但是当我使用下面的in_array检查主题值时,它将显示否定结果。
foreach ($cart_info as $item){
foreach ($item as $item2){
if (in_array("Computer", $item2['subject'])) {
echo "Yes";
}else{
echo "No";
}
}
}
答案 0 :(得分:0)
haystack应该是一个数组
请查看in_array
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
haystack
The array.
strict
If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.
答案 1 :(得分:0)
您传递的是错误的密钥,因此无法获得所需的输出。
应该是
in_array("Computer", $cart_info[0])
或
in_array("Computer", $cart_info)
甚至
if($item2 == "Computer")
而不是
in_array("Computer", $item2['subject'])
答案 2 :(得分:0)
试
if (in_array("Computer", $item2)) {
echo "Yes";
}else{
echo "No";
}
答案 3 :(得分:0)
因为$item2['subject']
不是数组。在您的情况下使用:
foreach ($cart_info as $item){
foreach ($item as $item2){
if ("Computer" == $item2['subject'])) {
echo "Yes";
}else{
echo "No";
}
}
}