合并多维数组的值

Combine value of multidimensional Array

我有这样的多维数组输出 如下所示,其中

我想使用任何分隔符组合 pidmap 的值,但不使用 comma(,),其中 id 相同

这是一个样本数据数组,有超过 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. 确定合并的层级,即当前数组项中没有'children'键时;在这种情况下,我们必须执行递归步骤来检查更深层的子元素。

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

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

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

代码:

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