组合多维数组的值


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

我也尝试过从这个解决方案检查这个

感谢提前让我知道是否有任何功能

没有一个函数可以为您做到这一点。我采取的方法是:

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