如何删除不要在PHP数组包含一定的价值分支机构 [英] How to remove branches that don't contain a certain value in a php array

查看:82
本文介绍了如何删除不要在PHP数组包含一定的价值分支机构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一天deceze的回答打不过我要使其工作毫无进展。我可能有它的一部分,但不知道如何在array_filter得到递归。

我的数组看起来像这样(样品):

 阵列

    [名] =>根
    [ChildCats] =>排列
        (
            [0] =>排列
                (
                    [名] =>空调
                    [ChildCats] =>排列
                        (
                            [0] =>排列
                                (
                                    [名] =>管道式空调
                                    [ChildCats] =>排列
                                        (
                                            [0] =>排列
                                                (
                                                    [名] =>供应及放大器;安装
                                                    [ChildCats] =>排列
                                                        (
                                                            [0] =>排列
                                                                (
                                                                    [名] =>大研
                                                                    [S] => 6067
                                                                )                                                        )                                                )                                            [1] =>排列
                                                (
                                                    [名] =>只有供应
                                                    [ChildCats] =>排列
                                                        (
                                                            [0] =>排列
                                                                (
                                                                    [名] =>三菱
                                                                    [S] => 6026
                                                                )                                                        )                                                )                                        )                                )                            [1] =>排列
                                (
                                    [名] =>拆分系统空调
                                    [ChildCats] =>排列
                                        (
                                            [0] =>排列
                                                (
                                                    [名] =>供应及放大器;安装
                                                    [ChildCats] =>排列
                                                        (
                                                            [0] =>排列
                                                                (
                                                                    [名] =>大研
                                                                    [S] => 6067
                                                                )                                                            [1] =>排列
                                                                (
                                                                    [名] =>富士通分体式空调系统
                                                                    [S] => 6464
                                                                )                                                            [2] =>排列
                                                                (
                                                                    [名] =>三菱电机分体式空调系统
                                                                    [S] => 6464
                                                                )                                                        )                                                )                                        )                                )                        )                )            [1] =>排列
                (
                    [名] =>电器/白色家电
                    [ChildCats] =>排列
                        (
                            [0] =>排列
                                (
                                    [名] =>净空
                                    [S] => 6239
                                )                            [1] =>排列
                                (
                                    [名] =>灶具
                                    [ChildCats] =>排列
                                        (
                                            [0] =>排列
                                                (
                                                    [名] =>陶瓷灶具
                                                    [S] => 6239
                                                )                                            [1] =>排列
                                                (
                                                    [名] =>元素灶具
                                                    [S] => 6067
                                                )                                            [2] =>排列
                                                (
                                                    [名] =>燃气灶具
                                                    [S] => 6239
                                                )                                            [3] =>排列
                                                (
                                                    [名] =>电磁灶
                                                    [S] => 6239
                                                )                                        )                                )
                        )                )

现在可以说,我尽量只提取数组培训相关的下列密钥对的部分:

S => 6067

我想结果看起来像:

 阵列

    [名] =>根
    [ChildCats] =>排列
        (
            [0] =>排列
                (
                    [名] =>空调
                    [ChildCats] =>排列
                        (
                            [0] =>排列
                                (
                                    [名] =>管道式空调
                                    [ChildCats] =>排列
                                        (
                                            [0] =>排列
                                                (
                                                    [名] =>供应及放大器;安装
                                                    [ChildCats] =>排列
                                                        (
                                                            [0] =>排列
                                                                (
                                                                    [名] =>大研
                                                                    [S] => 6067
                                                                )                                                        )                                                )                                        )                                )                            [1] =>排列
                                (
                                    [名] =>拆分系统空调
                                    [ChildCats] =>排列
                                        (
                                            [0] =>排列
                                                (
                                                    [名] =>供应及放大器;安装
                                                    [ChildCats] =>排列
                                                        (
                                                            [0] =>排列
                                                                (
                                                                    [名] =>大研
                                                                    [S] => 6067
                                                                )
                                                        )                                                )                                        )                                )                        )                )            [1] =>排列
                (
                    [名] =>电器/白色家电
                    [ChildCats] =>排列
                        (                            [0] =>排列
                                (
                                    [名] =>灶具
                                    [ChildCats] =>排列
                                        (                                            [0] =>排列
                                                (
                                                    [名] =>元素灶具
                                                    [S] => 6067
                                                )
                                        )                                )
                        )                )
    )

我不能让我的头是以防万一我应该创建一个新的阵列或使用数组过滤器。

与deceze code我有使用搜索工作打以下内容:

 函数recursive_assoc_in_array(数组$大海捞针,数组$针,$ childKey ='ChildCats'){    如果(array_intersect_assoc($大海捞针,$针)){
        回声通过array_intersect_assoc找到$草堆[名]\\ n。
        返回true;
    }    的foreach($大海捞针[$ childKey]为$子){         如果(recursive_assoc_in_array($孩子,$针,$ childKey))返回true;
    }
    返回false;}

但是,如果我试图用处理,

  $阵列= array_filter($阵列功能(数组$值){
    返回recursive_assoc_in_array($值,数组('S'=>'6067'));
});

我得到的原始数组这使我想到我得递归的array_filter查询运行。

在这一点上,我只是一片空白。

此外,数组键将需要产生新的阵列上重建索引。任何想法?

- 其他14年7月7日

怎么样,如果我试图从旧建新阵列?

我想:

 函数exploreArrayandAdd($阵列){    如果($数组['ChildCats']){        的foreach($数组['ChildCats']为$关键=> $值){
            $ NewArray ['ChildCats'] [] = exploreArrayandAdd($值);
        }    }其他{        如果($数组['S'] == 6026){
            //回声1;
            返回$阵列;
        }    }}

但无法工作,如何新的数组传递出的功能?

试着删除那些不使用匹配分支:

 函数exploreArray(安培; $阵列){    如果($数组['ChildCats']){        的foreach($数组['ChildCats']为$关键=> $值){
            $结果= exploreArray($数组['ChildCats'] [$关键]);            如果($结果===假)
                未设置($数组['ChildCats'] [$关键]);        }    }其他{
        //的print_r($阵列);
        如果($数组['S'] == 6026){
            返回true;
        }其他{
            未设置($阵列);
            返回false;
        }    }
    //如果($ NoChildCat == true)而的print_r($阵列);}

不过,我认为这是错误的方式,因为它不工作在阵列的底部,但不会备份朝上方作为兄弟做的结果真。
同时这不会重新索引数组键。


解决方案

 函数recursive_array_filter(数组$数组$ childKey,调用$试){
    如果(使用isset($数组[$ childKey])及和放大器; is_array($数组[$ childKey])){
        的foreach($数组[$ childKey]为$关键=>&安培; $子){
            如果(!$ =子recursive_array_filter($孩子,$ childKey,$测试)){
                未设置($数组[$ childKey] [$关键]);
            }
        }
        如果(!$阵列[$ childKey]){
            未设置($数组[$ childKey]);
        }
    }
    回!空($数组[$ childKey])|| $测试($数组)? $数组:[];
}$阵列= recursive_array_filter($数组,'ChildCats',函数(数组$阵列){
    返回array_intersect_assoc($数组['S'=> 6026]);
});

要前preSS的话算法:你往下下降到数组第一个,后面的所有 ChildCats 跳转到他们的结局。在每个级别返回的值,因为它们返回给调用者,如果他们测试匹配的的,如果他们有孩子,或者你返回一个空数组(您也可以返回如果您preFER)。如果某些孩子原来空的,你修剪它取消设置

我在这里实现了测试作为一个回调函数为code的最好的可重用性。

I've spent the day playing with deceze's answer but I'm no closer to making it work. I may have part of it, but not sure how to get recursion in array_filter.

My Array looks like this (sample):

Array
(
    [name] => root
    [ChildCats] => Array
        (
            [0] => Array
                (
                    [name] => Air Conditioning
                    [ChildCats] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Ducted Air Conditioning
                                    [ChildCats] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Supply & Install
                                                    [ChildCats] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [name] => Daiken
                                                                    [S] => 6067
                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [name] => Supply Only
                                                    [ChildCats] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [name] => Mitsubishi
                                                                    [S] => 6026
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [name] => Split System Air Conditioning
                                    [ChildCats] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Supply & Install
                                                    [ChildCats] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [name] => Daiken
                                                                    [S] => 6067
                                                                )

                                                            [1] => Array
                                                                (
                                                                    [name] => Fujitsu Split Air Conditioning Systems
                                                                    [S] => 6464
                                                                )

                                                            [2] => Array
                                                                (
                                                                    [name] => Mitsubishi Electric Split Air Conditioning Systems
                                                                    [S] => 6464
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [name] => Appliance / White Goods
                    [ChildCats] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Clearance
                                    [S] => 6239
                                )

                            [1] => Array
                                (
                                    [name] => Cooktops
                                    [ChildCats] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Ceramic Cooktops
                                                    [S] => 6239
                                                )

                                            [1] => Array
                                                (
                                                    [name] => Element Cooktops
                                                    [S] => 6067
                                                )

                                            [2] => Array
                                                (
                                                    [name] => Gas Cooktops
                                                    [S] => 6239
                                                )

                                            [3] => Array
                                                (
                                                    [name] => Induction Cooktops
                                                    [S] => 6239
                                                )

                                        )

                                )


                        )

                )

Now lets say I try to extract just the parts of the array relevent to the following keypair:

S => 6067.

I'd like the result to look like:

Array
(
    [name] => root
    [ChildCats] => Array
        (
            [0] => Array
                (
                    [name] => Air Conditioning
                    [ChildCats] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Ducted Air Conditioning
                                    [ChildCats] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Supply & Install
                                                    [ChildCats] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [name] => Daiken
                                                                    [S] => 6067
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [name] => Split System Air Conditioning
                                    [ChildCats] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Supply & Install
                                                    [ChildCats] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [name] => Daiken
                                                                    [S] => 6067
                                                                )


                                                        )

                                                )

                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [name] => Appliance / White Goods
                    [ChildCats] => Array
                        (

                            [0] => Array
                                (
                                    [name] => Cooktops
                                    [ChildCats] => Array
                                        (

                                            [0] => Array
                                                (
                                                    [name] => Element Cooktops
                                                    [S] => 6067
                                                )


                                        )

                                )


                        )

                )
    )
)

What I cannot get my head arround is should I be creating a new array or using array filter.

Playing with deceze code I've got the search working using the following:

function recursive_assoc_in_array(array $haystack,  array $needle, $childKey = 'ChildCats') {

    if (array_intersect_assoc($haystack, $needle)) {
        echo "Found via array_intersect_assoc ".$haystack[name]."\n";
        return true;    
    } 

    foreach ($haystack[$childKey] as $child) {

         if (recursive_assoc_in_array($child, $needle, $childKey)) return true;
    }
    return false;

}

But if I try to process with,

$array = array_filter($array, function (array $values) {
    return recursive_assoc_in_array($values, array('S' => '6067'));
});

I get the original array which leads me to think I have to get recursion running on the array_filter query.

At this point I just go blank.

Additionally, the array keys will need to be reindexed on the produced new array. Any ideas?

--Additional 7/7/14

How about if I try to build a new array from the old one?

I'm trying:

function exploreArrayandAdd($Array) {

    if ($Array['ChildCats']) {

        foreach ($Array['ChildCats'] as $key => $value) {
            $NewArray['ChildCats'][] = exploreArrayandAdd($value);
        }

    } else {

        if ($Array['S'] == 6026) {
            //echo "1";
            return $Array;
        } 

    }

}

But cannot work out how to pass the new array out of the function?

Tried removing branches that don't match using:

function exploreArray(&$Array) {

    if ($Array['ChildCats']) {

        foreach ($Array['ChildCats'] as $key => $value) {
            $result = exploreArray($Array['ChildCats'][$key]);

            if ($result === false)
                unset($Array['ChildCats'][$key]);

        }

    } else {
        //  print_r($Array);
        if ($Array['S'] == 6026) {
            return true;
        } else {
            unset($Array);
            return false;
        }

    }
    //if ($NoChildCat==true) print_r($Array);

}

But I believe it is the wrong way as it does work at the bottom of the array but not back up towards the top as siblings make result true. Also this won't reindex the array keys.

解决方案

function recursive_array_filter(array $array, $childKey, callable $test) {
    if (isset($array[$childKey]) && is_array($array[$childKey])) {
        foreach ($array[$childKey] as $key => &$child) {
            if (!$child = recursive_array_filter($child, $childKey, $test)) {
                unset($array[$childKey][$key]);
            }
        }
        if (!$array[$childKey]) {
            unset($array[$childKey]);
        }
    }
    return !empty($array[$childKey]) || $test($array) ? $array : [];
}

$array = recursive_array_filter($array, 'ChildCats', function (array $array) {
    return array_intersect_assoc($array, ['S' => 6026]);
});

To express the algorithm in words: you descend down into the array first, following all ChildCats branches to their end. In each level you return the values as they are back to the caller if they match your test or if they have children, or you return an emptied array (you could also return false if you prefer). If some child turns out empty, you prune it with unset.

I have implemented the test as a callback function here for best reusability of the code.

这篇关于如何删除不要在PHP数组包含一定的价值分支机构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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