Recursive array_search

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

I have a multi-dimensional array:

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


        
相关标签:
4条回答
  • 2020-12-06 03:21
    public static function unique(array $data, $key){
        $rez = [];
        foreach($data as $val){
            if(!isset($val[$key]) && is_array($val)){
                return self::unique($val, $key);
            }elseif( isset($val[$key]) ){
                $rez[] = $val[$key];
            }
        }
        return array_unique($rez);
    }
    
    0 讨论(0)
  • 2020-12-06 03:24

    You can change your recursive function like this, which should give you the solution:

    function recursive_array_search($needle, $haystack, $currentKey = '') {
        foreach($haystack as $key=>$value) {
            if (is_array($value)) {
                $nextKey = recursive_array_search($needle,$value, $currentKey . '[' . $key . ']');
                if ($nextKey) {
                    return $nextKey;
                }
            }
            else if($value==$needle) {
                return is_numeric($key) ? $currentKey . '[' .$key . ']' : $currentKey . '["' .$key . '"]';
            }
        }
        return false;
    }
    

    This will result in

    [1][1][0]["CategoryID"]
    

    Since CategoryID is also a key in your multidimensional array.

    If you don't want this, you can adapt the function to

    function recursive_array_search($needle, $haystack, $currentKey = '') {
        foreach($haystack as $key=>$value) {
            if (is_array($value)) {
                $nextKey = recursive_array_search($needle,$value, $currentKey . '[' . $key . ']');
                if ($nextKey) {
                    return $nextKey;
                }
            }
            else if($value==$needle) {
                return is_numeric($key) ? $currentKey . '[' .$key . ']' : $currentKey;
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-06 03:26
    function array_search_recursive( $search, $values = array(), $i = 0) {
        $match = false;
        var_dump($i, $search);
        $i++;
        foreach ( $values as $keyState => $val ) {
            var_dump($val == $search, 'expression');
            if ( $val == $search ) {
                return $keyState;
            }
            if ( is_array( $val ) ) {
                $match = array_search_recursive($search, $val, $i);
            }
            if ( $match !== false ) {
                return $keyState;
            }
        }
    
        return false;
    }
    
    echo array_search_recursive($search, $canada)
    

    Edit:

    This will return the first key, tested for $canada = array( 'Brazilia' => 'test1', "Alberta" => [ "Airdrie", "Brooks", "Camrose" ], "British Columbia" => [ "Abbotsford" => [ 'a', 'b', 'c' ], "Armstrong", "Castlegar" ], "Manitoba" => [ "Brandon", "Selkirk" ], 'Olanda' => 'test2' ); $search = "Selkirk";

    0 讨论(0)
提交回复
热议问题