递归array_search

时间:2015-02-12 08:37:44

标签: php arrays search

我有一个多维数组:

$categories = array(
    array(
        'CategoryID' => 14308,
        'CategoryLevel' => 1,
        'CategoryName' => 'Alcohol & Food',
        'CategoryParentID' => 14308
    ),
    // CHILD CATEGORIES
    array(
        array(
            'CategoryID' => 179836,
            'CategoryLevel' => 2,
            'CategoryName' => 'Alcohol & Alcohol Mixes',
            'CategoryParentID' => 14308
        ),
        array(
            array(
                'CategoryID' => 172528,
                'CategoryLevel' => 2,
                'CategoryName' => 'Antipasto, Savoury',
                'CategoryParentID' => 14308
            )
        )
    )
);

我需要获取索引的确切位置,并且由于array_search不能处理多维数组,因此我使用了PHP手册页上提供的功能之一。

function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}

..但它也只返回第一个数组的键:

echo recursive_array_search(172528, $categories); // outputs 1

我期待:

[1][1][0]

4 个答案:

答案 0 :(得分:10)

您可以像这样更改递归函数,它应该为您提供解决方案:

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

这将导致

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

由于CategoryID也是多维数组中的一个键。

如果您不想这样,可以将功能调整为

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

答案 1 :(得分:2)

您忽略了对recursive_array_search内部调用的返回值。不要那样做。

/*
 * 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;
        }
    }
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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)

编辑:

这将返回经过$canada = array( 'Brazilia' => 'test1', "Alberta" => [ "Airdrie", "Brooks", "Camrose" ], "British Columbia" => [ "Abbotsford" => [ 'a', 'b', 'c' ], "Armstrong", "Castlegar" ], "Manitoba" => [ "Brandon", "Selkirk" ], 'Olanda' => 'test2' ); $search = "Selkirk";测试的第一个密钥