递归array_search [英] Recursive array_search

查看:145
本文介绍了递归array_search的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维阵列

  $类=阵列(
    阵列(
        '类别id => 14308,
        CategoryLevel'=> 1,
        '类别名称'=> 酒精和放大器;餐饮',
        CategoryParentID'=> 14308
    )
    //子类别
    阵列(
        阵列(
            '类别id => 179836,
            CategoryLevel'=> 2,
            '类别名称'=> 酒精和放大器;酒精混合料',
            CategoryParentID'=> 14308
        )
        阵列(
            阵列(
                '类别id => 172528,
                CategoryLevel'=> 2,
                '类别名称'=> 开胃,开胃,
                CategoryParentID'=> 14308
            )
        )
    )
);

我需要得到指标的准确位置,并自 array_search 不会对多维数组的工作,我使用所提供的功能之一PHP手册页。

 函数recursive_array_search($针,干草堆$){
    的foreach($大海捞针为$关键=> $值){
        $ current_key = $关键;
        如果($针=== $值或(is_array($值)及&放大器; recursive_array_search($针,$值)==假)!){
            返回$ current_key;
        }
    }
    返回false;
}

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

 回声recursive_array_search(172528,$类别); //输出1

我期待:

  [1] [1] [0]


解决方案

您可以更改您的递归函数就是这样,这应该给您的解决方案:

 函数recursive_array_search($针,干草堆$,$ currentKey =''){
    的foreach($大海捞针为$关键=> $值){
        如果(is_array($值)){
            ($针,价值$,$ currentKey。'['$键。']')$ = NEXTKEY recursive_array_search;
            如果($ NEXTKEY){
                返回$ NEXTKEY;
            }
        }
        否则如果($价值== $针){
            返回is_numeric($键)? $ currentKey。 '['。$键。 ']':$ currentKey。 ['。$键。']';
        }
    }
    返回false;
}

这将导致

  [1] [1] [0] [类别ID]

由于类别ID也是你的多维数组的关键。

如果你不希望这样,可以将功能适应

 函数recursive_array_search($针,干草堆$,$ currentKey =''){
    的foreach($大海捞针为$关键=> $值){
        如果(is_array($值)){
            ($针,价值$,$ currentKey。'['$键。']')$ = NEXTKEY recursive_array_search;
            如果($ NEXTKEY){
                返回$ NEXTKEY;
            }
        }
        否则如果($价值== $针){
            返回is_numeric($键)? $ currentKey。 '['。$键。 ']':$ currentKey;
        }
    }
    返回false;
}

I have a multi-dimensional array:

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

I need to get the exact location of the index, and since array_search doesn't work on multi-dimensional arrays, I'm using one of the functions provided on the PHP manual page.

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

.. but it also returns the key of the first array only:

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

I'm expecting:

[1][1][0]

解决方案

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

这篇关于递归array_search的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆