array_unique 5维数组php

时间:2011-09-21 13:49:30

标签: php arrays multidimensional-array array-unique

我正在寻找一种优雅的方法来重复演绎像thaht这样的5维数组:

Array
(
    [LOADERS] => Array
    (
        [S130] => Array
            (
                [527311001 & Above] => Array
                    (
                        [MAINTENANCE ITEMS] => Array
                            (
                                [0] => MAINTENANCE ITEMS
                                [1] => SCHEDULED MAINTENANCE ITEMS (50 HOUR)
                                [2] => SCHEDULED MAINTENANCE ITEMS (250 HOUR)
                                [3] => SCHEDULED MAINTENANCE ITEMS (1000 HOUR)
                                [4] => SCHEDULED MAINTENANCE ITEMS (1000 HOUR)
                                [5] => MAINTENANCE ITEMS
                                [6] => SCHEDULED MAINTENANCE ITEMS (50 HOUR)
                                [7] => SCHEDULED MAINTENANCE ITEMS (250 HOUR)
                                [8] => SCHEDULED MAINTENANCE ITEMS (500 HOUR)
                                [9] => SCHEDULED MAINTENANCE ITEMS (1000 HOUR)
                            )
                    )
            )
    )
)

这可能还是我应该使用5-nested foreach?

1 个答案:

答案 0 :(得分:2)

因为array_unique不适用于多维数组。

你必须编写自己的逻辑才能做到这一点:

  • 很多嵌套循环,就像你说的那样
  • 或像这样的array_unique的递归版本(taken from the documentation):

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