in_array() and multidimensional array

前端 未结 22 1327
眼角桃花
眼角桃花 2020-11-22 00:30

I use in_array() to check whether a value exists in an array like below,

$a = array(\"Mac\", \"NT\", \"Irix\", \"Linux\");
if (in_array(\"Irix\"         


        
相关标签:
22条回答
  • 2020-11-22 01:15

    I was looking for a function that would let me search for both strings and arrays (as needle) in the array (haystack), so I added to the answer by @jwueller.

    Here's my code:

    /**
     * Recursive in_array function
     * Searches recursively for needle in an array (haystack).
     * Works with both strings and arrays as needle.
     * Both needle's and haystack's keys are ignored, only values are compared.
     * Note: if needle is an array, all values in needle have to be found for it to
     * return true. If one value is not found, false is returned.
     * @param  mixed   $needle   The array or string to be found
     * @param  array   $haystack The array to be searched in
     * @param  boolean $strict   Use strict value & type validation (===) or just value
     * @return boolean           True if in array, false if not.
     */
    function in_array_r($needle, $haystack, $strict = false) {
         // array wrapper
        if (is_array($needle)) {
            foreach ($needle as $value) {
                if (in_array_r($value, $haystack, $strict) == false) {
                    // an array value was not found, stop search, return false
                    return false;
                }
            }
            // if the code reaches this point, all values in array have been found
            return true;
        }
    
        // string handling
        foreach ($haystack as $item) {
            if (($strict ? $item === $needle : $item == $needle)
                || (is_array($item) && in_array_r($needle, $item, $strict))) {
                return true;
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 01:16
    $userdb = Array
    (
        (0) => Array
            (
                ('uid') => '100',
                ('name') => 'Sandra Shush',
                ('url') => 'urlof100'
            ),
    
        (1) => Array
            (
                ('uid') => '5465',
                ('name') => 'Stefanie Mcmohn',
                ('url') => 'urlof5465'
            ),
    
        (2) => Array
            (
                ('uid') => '40489',
                ('name') => 'Michael',
                ('url') => 'urlof40489'
            )
    );
    
    $url_in_array = in_array('urlof5465', array_column($userdb, 'url'));
    
    if($url_in_array) {
        echo 'value is in multidim array';
    }
    else {
        echo 'value is not in multidim array';
    }
    
    0 讨论(0)
  • 2020-11-22 01:16

    The accepted solution (at the time of writing) by jwueller

    function in_array_r($needle, $haystack, $strict = false) {
        foreach ($haystack as $item) {
            if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
                return true;
            }
        }
    
        return false;
    }
    

    Is perfectly correct but may have unintended behaviuor when doing weak comparison (the parameter $strict = false).

    Due to PHP's type juggling when comparing values of different type both

    "example" == 0
    

    and

    0 == "example"
    

    Evaluates true because "example" is casted to int and turned into 0.

    (See Why does PHP consider 0 to be equal to a string?)

    If this is not the desired behaviuor it can be convenient to cast numeric values to string before doing a non-strict comparison:

    function in_array_r($needle, $haystack, $strict = false) {
        foreach ($haystack as $item) {
    
            if( ! $strict && is_string( $needle ) && ( is_float( $item ) || is_int( $item ) ) ) {
                $item = (string)$item;
            }
    
            if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
                return true;
            }
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 01:17

    Here is my proposition based on json_encode() solution with :

    • case insensitive option
    • returning the count instead of true
    • anywhere in arrays (keys and values)

    If word not found, it still returns 0 equal to false.

    function in_array_count($needle, $haystack, $caseSensitive = true) {
        if(!$caseSensitive) {
            return substr_count(strtoupper(json_encode($haystack)), strtoupper($needle));
        }
        return substr_count(json_encode($haystack), $needle);
    }
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-22 01:18

    you can use like this

    $result = array_intersect($array1, $array2);
    print_r($result);
    

    http://php.net/manual/tr/function.array-intersect.php

    0 讨论(0)
  • 2020-11-22 01:20

    You could always serialize your multi-dimensional array and do a strpos:

    $arr = array(array("Mac", "NT"), array("Irix", "Linux"));
    
    $in_arr = (bool)strpos(serialize($arr),'s:4:"Irix";');
    
    if($in_arr){
        echo "Got Irix!";
    }
    

    Various docs for things I used:

    • strpos()
    • serialize()
    • Type Juggling or (bool)
    0 讨论(0)
提交回复
热议问题