Can I use PHP's array_search() function to search for situations where the values ​​themselves are arrays?
P粉798343415
P粉798343415 2023-07-19 23:47:04
0
1
569

I have a class that contains a static array whose keys are strings and values ​​are arrays. If a specific array value exists, I want to get its key.

I thought the code using the array_search() function would accomplish this, but it fails to find the value and instead prints false. I thought the "strict" parameter might make a difference, but it doesn't seem to work. Did I do something wrong? What's the best way to write search code?

$ cat foo.php
<?php
class foo {
    static $name_to_bitnum = array(
        'Water Obstacle' => array('kind' => 'Facility', 'bitnum' => 5),
        'Driving' => array('kind' => 'Facility', 'bitnum' => 6),
        'Trails' => array('kind' => 'Facility', 'bitnum' => 7),
   );

    public static function bar($kind, $bitnum) {
        $search = array_search(array('kind' => $kind, 'bitnum' => $bitnum), self::$name_to_bitnum, $strict = false);
        // $search = array_search(self::$name_to_bitnum['Driving'], self::$name_to_bitnum, $strict = false);
        return $search;
    }
}
$foo = new foo();
echo var_dump($foo->bar('Driving', 6));
?>

$ php foo.php
C:xampp1826htdocsOSH0foo.php:21:
bool(false)

I noticed that if I uncommented the line of code and indexed into the array using the value of the array element I was looking for, then it succeeded and printed out the string (7) "Driving" . This doesn't seem right to me.

P粉798343415
P粉798343415

reply all(1)
P粉614840363

As pointed out in the comments, my test case is passing 'Driving' when calling bar() when it should actually be passing 'Facility'. Now I just need to go back to the original code and see why the wrong value was passed. Very embarrassing and sorry for causing trouble to you.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!