繁体   English   中英

递归array_search

[英]Recursive array_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]

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

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

您忽略了对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;
        }
    }
}
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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM