结合多维数组的值

时间:2015-03-26 10:10:21

标签: php arrays multidimensional-array combiners

我有像这样的多维数组输出如下所示

我希望使用任何分隔符合并pidmap的值,但不comma(,) id {/ 1}}

这是一个样本数据数组有超过20000个值,深度级别未知可能是18或20

 Array
(
[pid] => 10000
[map] => 11, 11
[id] => 5740
[parentId] => 5739
[text] => Text1
[children] => Array
  (
    [0] => Array
      (
        [pid] => 600
        [map] => 
        [id] => 5741
        [parentId] => 5740
        [text] => Adv
        [children] => Array
         (
            [0] => Array
             (
                [pid] => 620
                [map] => 115.43271636963, 28
                [id] => 5745
                [parentId] => 5741
                [text] => Text1.1
              )
            [1] => Array
              (
                [pid] => 621
                [map] => 1, 2
                [id] => 5745
                [parentId] => 5741
                [text] => Text1.1
              )
            [2] => Array
             (
                [pid] => 1748
                [map] => 11.43, 28
                [id] => 5746
                [parentId] => 5741
                [text] => Text1.2
             ) 
          )
      )
    [1] => Array
     (
        [pid] => 700
        [map] => 15, 17
        [id] => 5750
        [parentId] => 5740
        [text] => Text2
        [children] => Array
         (
            [0] => Array
             (
                [pid] => 500
                [map] => 139.525390625, 35.797920227051
                [id] => 5751
                [parentId] => 5750
                [text] => Text2.1
              )
            [1] => Array
             (
                [pid] => 502
                [map] => 15, 17
                [id] => 5751
                [parentId] => 5750
                [text] => Text2.1
              )
            [2] => Array
             (
                [pid] => 1157
                [map] => 7.8320698738098, 48.023639678955
                [id] => 5754
                [parentId] => 5750
                [text] => Text2.2
             )
         )    
     )
  )
)

预期产量是。我在这里使用:作为分隔符

 Array
(
[pid] => 10000
[map] => 11, 11
[id] => 5740
[parentId] => 5739
[text] => Text1
[children] => Array
  (
    [0] => Array
      (
        [pid] => 600
        [map] => 
        [id] => 5741
        [parentId] => 5740
        [text] => Adv
        [children] => Array
         (
            [0] => Array
             (
                [pid] => 620 : 621
                [map] => 115.43271636963, 28 : 1, 2
                [id] => 5745
                [parentId] => 5741
                [text] => Text1.1
              )
            [1] => Array
             (
                [pid] => 1748
                [map] => 11.43, 28
                [id] => 5746
                [parentId] => 5741
                [text] => Text1.2
             ) 
          )
      )
    [1] => Array
     (
        [pid] => 700
        [map] => 15, 17
        [id] => 5750
        [parentId] => 5740
        [text] => Text2
        [children] => Array
         (
            [0] => Array
             (
                [pid] => 500 : 502
                [map] => 139.525390625, 35.797920227051 : 15, 17
                [id] => 5751
                [parentId] => 5750
                [text] => Text2.1
              )
            [1] => Array
             (
                [pid] => 1157
                [map] => 7.8320698738098, 48.023639678955
                [id] => 5754
                [parentId] => 5750
                [text] => Text2.2
             )
         )    
     )
  )
)

我尝试了很多代码

其中一个功能是array_merge_recursive

另外

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

我也试过这个解决方案Check this

先谢谢,如果有任何功能,请告诉我

1 个答案:

答案 0 :(得分:0)

没有任何一项功能可以为您做到这一点。我采取的方法是:

  1. 确定合并的级别,即当前数组项中没有'children'键的时间;在这种情况下,我们必须执行递归步骤来检查更深层的子元素。

  2. 遍历子元素,如果它包含'children'键,则执行另一个递归步骤。

  3. 构建一个临时数组,为子元素中的每个键保留所有可能的值,并从主数组中删除这些元素。

  4. 使用临时数组中的元素重建主数组。

  5. 代码:

    function recursive_group(array &$arr)
    {
        if (array_key_exists('children', $arr)) {
            // 1. perform recursive step
            recursive_group($arr['children']);
            return;
        }
    
        $groups = [];
        foreach ($arr as $key => &$subarr) {
            if (array_key_exists('children', $subarr)) {
                // 2. perform another recursive step
                recursive_group($subarr['children']);
            } elseif (isset($subarr['id'])) {
                // 3. build temporary array
                $id = $subarr['id'];
                foreach ($subarr as $param_name => $param_value) {
                    if ($param_name != 'id') {
                        $groups[$id][$param_name][] = $param_value;
                    }
                }
                // 3. remove from main array
                unset($arr[$key]);
            }
        }
    
        if ($groups) {
            // 4. rebuild main array
            foreach ($groups as $id => $params) {
                $tmp = ['id' => $id];
                foreach ($params as $param_name => $param_values) {
                    $tmp[$param_name] = join(' : ', $param_values);
                }
                $arr[] = $tmp;
            }
        }
    }