Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:数组值的比较不起作用?_Php_For Loop_Foreach - Fatal编程技术网

PHP:数组值的比较不起作用?

PHP:数组值的比较不起作用?,php,for-loop,foreach,Php,For Loop,Foreach,我试图比较数组值,如果找到值if(isset($row['height'])==['3455'])则打印数组值,但它不起作用。它返回所有数组,其中也包括不匹配的值。 如何比较一个值和如果找到值,则打印数组的单个值而不是所有值 这是我的打印值 Array ( [0] => Array ( [id] => 1 [name] => Bradeley Hall Pool [postcode]

我试图比较数组值,如果找到值
if(isset($row['height'])==['3455'])
则打印数组值,但它不起作用。它返回所有数组,其中也包括不匹配的值。 如何比较一个值和如果找到值,则打印数组的单个值而不是所有值

这是我的打印值

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Bradeley Hall Pool
            [postcode] => CW1 5QN
            [lat] => 53.10213
            [lon] => -2.41069
            [size] => 1.60
            [pegs] => 21
            [distance] => 26.6
            [suitability] => Array
                (
                    [0] => Array
                        (
                            [species] => Barbel
                            [species] => 1
                            [record] => 1
                            [weight] => 2.721554
                            [length] => 40
                            [height] => ['abc','345m','3455']
                        )
                )
        )

    [1] => Array
        (
            [id] => 2
            [name] => Farm Pool
            [postcode] => CW9 6JQ
            [lat] => 53.320502
            [lon] => -2.549049
            [size] => 0.88
            [pegs] => 8
            [distance] => 15.4
            [suitability] => Array
                (
                    [0] => Array
                        (
                            [species] => Barbel
                            [species] => 1
                            [record] => 1
                            [weight] => 2.721554
                            [length] => 40
                            [height] => ['33','3455','3mnc']
                        )
                )
       )
) 
我的代码-

foreach( $cursor as $row){
 
    foreach ($row['suitability'] as  $item) {
  
        if(isset($item['height']) == ['3455']){
            echo 'yes';
            echo '<pre>';
            print_r($item['height']);
        } else{
            echo 'no';
        }
    }
} 
foreach($cursor作为$row){
foreach($row['fitability']作为$item){
如果(isset($item['height'])=['3455']){
回应“是”;
<?php
foreach($cursor as $row){

    // Check if the row has the key `suitability`.
    if (isset($row['suitability'])) {

        foreach ($row['suitability'] as $item) {


            // Check if the item has the key `height`.
            if (isset($item['height'])) {

                 // Assuming `height` is always an array.
                 if (in_array('3455', $item['height']) {
                     // Yes.
                 }
            }
        }
    } 
}
回声';
<?php
foreach($cursor as $row){

    // Check if the row has the key `suitability`.
    if (isset($row['suitability'])) {

        foreach ($row['suitability'] as $item) {


            // Check if the item has the key `height`.
            if (isset($item['height'])) {

                 // Assuming `height` is always an array.
                 if (in_array('3455', $item['height']) {
                     // Yes.
                 }
            }
        }
    } 
}
打印($item['height']); }否则{ 回应‘否’;
<?php
foreach($cursor as $row){

    // Check if the row has the key `suitability`.
    if (isset($row['suitability'])) {

        foreach ($row['suitability'] as $item) {


            // Check if the item has the key `height`.
            if (isset($item['height'])) {

                 // Assuming `height` is always an array.
                 if (in_array('3455', $item['height']) {
                     // Yes.
                 }
            }
        }
    } 
}
} } }
有较短的方法,但您应该使用
isset
检查数组键是否存在。一旦知道它存在,就可以在数组中
检查
高度
数组中的特定值

foreach( $cursor as $row){
    foreach ($row['suitability'] as $suit) {
        // can we find the string in there somewhere
        if (strpos($suit['height'], '3455') !== false) {
            echo 'yes  ';
            echo $suit['height'];
            echo PHP_EOL;
        } else{
            echo 'no';
        }
    }
} 

我认为最明显的问题可能是
[height]=>['abc','345m','3455']
不是数组,否则
print\u r()
会将其显示为一个数组。所以它一定是一根弦

所以这是可行的


isset
返回布尔值(TRUE或FALSE)因此,如果要检查数组中是否有值-,它将永远不会等于包含一项任何内容的数组。因为它给出了错误
未定义索引:height
,所以我将如何进行比较?在某些情况下,height数组也包含单个值,而不是多个值array@RiggsFolly ,由于PHP对非严格比较进行了奇怪的转换:)(我的意思是这样做没有任何意义,只是说。)而且您没有
$row['height']
,但是您可能有一个
$item['height']
它不起作用,在数组中不给出错误
[height]=>['abc'、'345m'、'3455']
)高度是一个数组。它有时不是数组吗?