简体   繁体   中英

PHP: Comparison of array value not working?

I am trying to comparing array value, if value found if(isset($row['height']) == ['3455']) then print the array value but it is not working it is return all the array which includes not matching value too. How will I compare a value and if value found then print that single value of array not all value .

Here is my print_r value

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']
                        )
                )
       )
) 

My code -

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';
        }
    }
} 

I think the most obvious issue may be that [height] => ['abc','345m','3455'] is not an array, otherwise the print_r() would have shown it as one. So it must be a string.

So this would work

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';
        }
    }
} 

There are shorter ways, but you should use isset to check for the existence of the array key. Once you know it is present, you can in_array to check for a specific value within the height array.

<?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.
                 }
            }
        }
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM