PHP - 从数组中删除相同的兄弟(叶子)(从数组中删除相同的数组)

时间:2017-06-15 08:59:48

标签: php arrays

尝试从嵌套数组中删除相同的兄弟(叶子)/相同的数组。

e.g

$data = [
        'test' => [
                'a' => [
                        'b' => 'something',
                        5 => [
                                'a' => [
                                        1 => 'test1',
                                        19 => 'test2',
                                        6 => 'test3',
                                ],
                                0 => 'test',
                        ],
                ],
                'b' => 1,
                2 => [
                        3 => 'something',
                        5 => 'somethingelse',
                ],
                4 => 'body'
        ],
        'anothertest' => [
                'b' => 1,
                0 => [
                        'test' => 1,
                        2 => 'something',
                        3 => 'somethingelse',
                ],
                1 => [
                        'test' => 1,
                        2 => 'something',
                        3 => 'somethingelse',
                ],
        ],
];

Array
(
    [test] => Array
        (
            [a] => Array
                (
                    [b] => something
                    [5] => Array
                        (
                            [a] => Array
                                (
                                    [1] => test1
                                    [19] => test2
                                    [6] => test3
                                )

                            [0] => test
                        )

                )

            [b] => 1
            [2] => Array
                (
                    [3] => something
                    [5] => somethingelse
                )

            [4] => body
        )

    [anothertest] => Array
        (
            [b] => 1
            [0] => Array
                (
                    [test] => 1
                    [2] => something
                    [3] => somethingelse
                )

            [1] => Array
                (
                    [test] => 1
                    [2] => something
                    [3] => somethingelse
                )

        )

)

$ data ['anothertest'] [0]和$ data ['anothertest'] [1]是相同的,因此必须将其删除。

可以跳过带字符串索引的数组。

Bumped如何在foreach键值块中比较一个数组与另一个数组。

我知道我可以将相同的数组与===运算符进行比较,但不知道如何在foreach循环中访问下一个数组。

这是我的代码,带有PSUEDOCODE块,我不知道该怎么做。

function cleansiblings($array)
  {
    foreach ($array as $key => $value) {
                if (!is_string($key)) {
                   //PSEUDO CODE 
                   //compare current $value to $value+1?? 
                }
    }

  }

感谢任何帮助。

谢谢,

2 个答案:

答案 0 :(得分:0)

您可以删除多维数组中的重复项

<?php

function get_unique($array){
    $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
    foreach ($result as $key => $value){
        if ( is_array($value) ){
            $result[$key] = get_unique($value);
        }
    }
    return $result;
}

echo "<pre/>";print_r(get_unique($data));

?>

输出: - https://eval.in/817099

答案 1 :(得分:0)

回答问题的最后部分:您可以使用$ key访问foreach循环中的原始$数组:

function cleansiblings($array)
  {
    foreach ($array as $key => $value) {
            if (!is_string($key)) {
               if (isset($array[$key+1])) {
                  // Compare $array[$key] (or $value, it is the same) with $array[$key+1]
                  // To remove item from array, just unset it: unset($array[$key]). 
                  // Note that you can alter iterated array in PHP, 
                  // but not in some other languages.
               }
            }
    }

}