PHP -深度混合索引/关联数组中的未设置元素

时间:2021-05-14 21:18:57

I have a mixed index/associative array in PHP that can, in theory, be infinitely deep because some elements may have 'children' arrays. I attach a slimmed down example. The php array is derived from some JSON data.

我在PHP中有一个混合的索引/关联数组,理论上它可以无限深入,因为有些元素可能有“子数组”。我附上一个精简的例子。php数组来自一些JSON数据。

I want to search on a given key/value pair and to delete any elements matching that pair at the index level.

我想搜索给定的键/值对,并删除索引级别上与该键/值对匹配的任何元素。

So for example, in the array below, I wish to search for the key of 'formId' and a value of '44' and to delete elements such that $array[0][0][0][0] and $array[0][1] are removed. Obviously I will then need to re-number the array.

例如,在下面的数组中,我希望搜索'formId'的键和'44'的值,并删除诸如$array[0][0][0][0]和$array[0][1]之类的元素。显然,我需要重新给数组编号。

I need to be able to search on any key/value pair... only one pair at a time is necessary... that is to say that the above example of 'formId' of '44' might be 'viewId' of '16' next time.

我需要能够搜索任何键/值对……每次只需要一对……也就是说,上述“44”的“formId”的例子可能是“16”下一次的“viewId”。

I have tried numerous ways to do this, with iterators, recursive foreach loops, &$reference keys but I just cannot seem to get it right.

我尝试了很多方法来实现这一点,迭代器、每个循环的递归和$reference键,但是我似乎无法正确地完成它。

Array
    (
    [0] => Array
    (
        [formId] => 0
        [subId] => 0
        [viewId] => 0
        [id] => 0
        [children] => Array
        (
            [0] => Array
            (
                [formId] => 1
                [subId] => 0
                [viewId] => 16
                [id] => _st_node_5328_0_0
                [children] => Array
                (
                    [0] => Array
                    (                                           
                        [formId] => 1
                        [subId] => 0
                        [viewId] => 16  
                        [id] => _st_node_3838_0_0_0
                        [children] => Array
                        (
                            [0] => Array
                            (
                                [formId] => 44
                                [subId] => 
                                [viewId] => 16   
                                [id] => _st_node_947_0_0_0_0
                            )

                            [1] => Array
                            (   
                                [formId] => 7
                                [subId] => 
                                [viewId] => 16
                                [id] => _st_node_947_0_0_0_1

                            )

                            [2] => Array
                            (
                                [formId] => 3
                                [subId] => 
                                [viewId] => 16
                                [id] => _st_node_947_0_0_0_2 
                            )

                            [3] => Array
                            (
                                [formId] => 10
                                [subId] => 
                                [viewId] => 16
                                [id] => _st_node_947_0_0_0_3
                            )

                            [4] => Array
                            (
                                [formId] => 9
                                [subId] => 
                                [viewId] => 16                                                             
                                [id] => _st_node_947_0_0_0_4
                            )

                            [5] => Array
                            (
                                [formId] => 8
                                [subId] => 
                                [viewId] => 16
                                [id] => _st_node_947_0_0_0_5
                            )

                            [6] => Array
                            (
                                [formId] => 6
                                [subId] => 
                                [viewId] => 16
                                [id] => _st_node_947_0_0_0_6
                            )

                            [7] => Array
                            (                    
                                [formId] => 5
                                [subId] => 
                                [viewId] => 16
                                [id] => _st_node_947_0_0_0_7
                            )

                            [8] => Array
                            (
                                [relId] => 167
                                [formId] => 4
                                [text] => Laptop Computers
                                [subId] => 
                                [viewId] => 16
                                [id] => _st_node_947_0_0_0_8
                                [isActive] => 
                            )

                            [9] => Array
                            (
                                [formId] => 1
                                [subId] => 
                                [viewId] => 16
                                [id] => _st_node_947_0_0_0_9
                            )
                        )
                    )

                    [1] => Array
                    (
                        [formId] => 1
                        [subId] => 0
                        [viewId] => 1
                        [id] => _st_node_3838_0_0_1
                    )

                    [2] => Array
                    (
                        [formId] => 1
                        [subId] => 0
                        [viewId] => 11
                        [id] => _st_node_3838_0_0_2
                    )

                    [3] => Array
                    (
                        [formId] => 1
                        [subId] => 0
                        [viewId] => 12
                        [id] => _st_node_3838_0_0_3
                    )

                    [4] => Array
                    (
                        [formId] => 1
                        [subId] => 0
                        [viewId] => 13
                        [id] => _st_node_3838_0_0_4
                    )

                    [5] => Array
                    (
                        [formId] => 1
                        [subId] => 0
                        [viewId] => 14
                        [id] => _st_node_3838_0_0_5
                    )

                    [6] => Array
                    (
                        [formId] => 1
                        [subId] => 0
                        [viewId] => 110
                        [id] => _st_node_3838_0_0_6
                    )
                )
            )

            [1] => Array
            (
                [formId] => 44
                [subId] => 0
                [viewId] => 144
                [id] => _st_node_5328_0_1
            )

            [2] => Array
            (
                [formId] => 10
                [subId] => 0
                [viewId] => 26
                [id] => _st_node_5328_0_2
            )

            [3] => Array
            (
                [formId] => 9
                [subId] => 0
                [viewId] => 9
                [id] => _st_node_5328_0_3
            )

            [4] => Array
            (
                [formId] => 8
                [subId] => 0
                [viewId] => 8
                [id] => _st_node_5328_0_4
            )

            [5] => Array
            (
                [formId] => 7
                [subId] => 0
                [viewId] => 25
                [id] => _st_node_5328_0_5
            )

            [6] => Array
            (
                [formId] => 6
                [subId] => 0
                [viewId] => 6
                [id] => _st_node_5328_0_6
            )

            [7] => Array
            (
                [formId] => 5
                [subId] => 0
                [viewId] => 5
                [id] => _st_node_5328_0_7
            )

            [8] => Array
            (
                [formId] => 4
                [subId] => 0
                [viewId] => 4
                [id] => _st_node_5328_0_8
            )

            [9] => Array
            (
                [formId] => 3
                [subId] => 0
                [viewId] => 3
                [id] => _st_node_5328_0_9
            )

        )

    )
)

1 个解决方案

#1


1  

The idea

Since you want to unset elements, you definitely want to pass the $array into the function as a reference (&$array).

因为您希望取消设置元素,所以您肯定希望将$数组作为引用(&$array)传递给函数。

You want to use recursion to allow you to dig as deep as necessary based on the actual array.

您希望使用递归,以便根据实际的数组尽可能深入地挖掘。

You loop over all elements of the array and the first thing you do is check whether an element is an array. If it is, you check if that subarray matches your criteria. If it does, you remove it. If it doesn't you invoke the function with the same $key, $value pair on that subarray.

对数组的所有元素进行循环,首先要检查元素是否为数组。如果是,则检查该子数组是否匹配您的条件。如果是的话,你就把它拿走。如果没有,您可以使用相同的$key调用函数,即子数组上的$value对。

The code

function removeMatching($key, $value, &$arr) {
    // Iterate over all $arr elements
    foreach ($arr as $i => $subarr) {

        // Skip all elements that are not arrays
        if (is_array($subarr)) {

            // Check if the sub-array matches the criteria
            if (isset($subarr[$key]) && $subarr[$key] === $value) {

                // Remove it from the main array if it does
                unset($arr[$i]);
            } else {

                // If the sub-array didn't match the criteria
                // Check if it contains any other matching sub-arrays
                removeMatching($key, $value, $arr[$i]);
            }
        }
    }
}

Apply like this:

应用:

$array = []; // your array

removeMatching('formId', 44, $array);

var_dump($array);

#1


1  

The idea

Since you want to unset elements, you definitely want to pass the $array into the function as a reference (&$array).

因为您希望取消设置元素,所以您肯定希望将$数组作为引用(&$array)传递给函数。

You want to use recursion to allow you to dig as deep as necessary based on the actual array.

您希望使用递归,以便根据实际的数组尽可能深入地挖掘。

You loop over all elements of the array and the first thing you do is check whether an element is an array. If it is, you check if that subarray matches your criteria. If it does, you remove it. If it doesn't you invoke the function with the same $key, $value pair on that subarray.

对数组的所有元素进行循环,首先要检查元素是否为数组。如果是,则检查该子数组是否匹配您的条件。如果是的话,你就把它拿走。如果没有,您可以使用相同的$key调用函数,即子数组上的$value对。

The code

function removeMatching($key, $value, &$arr) {
    // Iterate over all $arr elements
    foreach ($arr as $i => $subarr) {

        // Skip all elements that are not arrays
        if (is_array($subarr)) {

            // Check if the sub-array matches the criteria
            if (isset($subarr[$key]) && $subarr[$key] === $value) {

                // Remove it from the main array if it does
                unset($arr[$i]);
            } else {

                // If the sub-array didn't match the criteria
                // Check if it contains any other matching sub-arrays
                removeMatching($key, $value, $arr[$i]);
            }
        }
    }
}

Apply like this:

应用:

$array = []; // your array

removeMatching('formId', 44, $array);

var_dump($array);