Recursive array_search

后端 未结 4 1109
终归单人心
终归单人心 2020-12-06 02:40

I have a multi-dimensional array:

$categories = array(
    array(
        \'CategoryID\' => 14308,
        \'CategoryLevel\' => 1,
        \'CategoryNa         


        
4条回答
  •  悲&欢浪女
    2020-12-06 03:26

    You are ignoring the returned value of your inner call to recursive_array_search. Don't do that.

    /*
     * Searches for $needle in the multidimensional array $haystack.
     *
     * @param mixed $needle The item to search for
     * @param array $haystack The array to search
     * @return array|bool The indices of $needle in $haystack across the
     *  various dimensions. FALSE if $needle was not found.
     */
    function recursive_array_search($needle,$haystack) {
        foreach($haystack as $key=>$value) {
            if($needle===$value) {
                return array($key);
            } else if (is_array($value) && $subkey = recursive_array_search($needle,$value)) {
                array_unshift($subkey, $key);
                return $subkey;
            }
        }
    }
    

提交回复
热议问题