如何使用点表示法在 PHP 中展开数组

kac*_*har 3 php arrays recursion

我有这样的 php 数组结构:

array(
    'servicemanagement.scheduler.events.edit' => 'Edit',
    'servicemanagement.scheduler.events.delete' => 'Delete',
    'servicemanagement.scheduler.events' => 'Events',
    'servicemanagement.scheduler' => 'Scheduler',
    'servicemanagement.subscribers' => 'Subscribers',
    'servicemanagement.subscribers.index' => 'Index',
    'servicemanagement' => 'Service management',
);
Run Code Online (Sandbox Code Playgroud)

我想将其转换为多维数组,例如:

array(
    'servicemanagement' => array(
        'id' => 'servicemanagement',
        'title' => 'Service Management',
        'children' => array(
            'scheduler' => array(
                'id' => 'servicemanagement.scheduler',
                'title' => 'Scheduler',
                'children' => array(
                    'events' => array(
                        'id' => 'servicemanagement.scheduler.events',
                        'title' => 'Events',
                        'children' => array(
                            'edit' => array(
                                'id' => 'servicemanagement.scheduler.events.edit',
                                'title' => 'Edit',
                                'children' => array(),
                            ),
                            'delete' => array(
                                'id' => 'servicemanagement.scheduler.events.delete',
                                'title' => 'Delete',
                                'children' => array(),
                            ),
                        ),
                    ),
                ),
            ),
            'subscribers' => array(
                'id' => 'servicemanagement.subscribers',
                'title' => 'Subscribers',
                'children' => array(
                    'index' => array(
                        'id' => 'servicemanagement.subscribers.index',
                        'title' => 'Index',
                    )
                ),
            ),
        ),
    ),
);
Run Code Online (Sandbox Code Playgroud)

我已经检查了一些答案,例如: How to set a deep array in PHP

但似乎我无法清除数组顶部的文字,最后一条记录“服务管理”删除了所有以前的记录。

那里使用的函数是

function setArray(&$array, $keys, $value) {
    $keys = explode(".", $keys);
    $current = &$array;
    foreach($keys as $key) {
        $current = &$current[$key];
    }
    $current = $value;
}
Run Code Online (Sandbox Code Playgroud)

我发现的另一个函数但它没有达到预期的结果是:

function unflatten($array,$prefix = '')
{
    $result = array();
    foreach($array as $key=>$value)    {
        if (!empty($prefix)) {
            $key = preg_replace('#^'.preg_quote($prefix).'#','',$key);
        }
        if (strpos($key,'.') !== false) {
            parse_str('result['.str_replace('.','][',$key)."]=".$value);
        } else {
            $result[$key] = $value;
        }
    }
    return $result;
}
Run Code Online (Sandbox Code Playgroud)

可以选择使用递归来展开此数组,因为所有记录的结束格式都相同。

有人可以给我这个提示吗?

And*_*ndy 5

unflatten在这里创建了一个函数供参考:

https://gist.github.com/Gerst20051/b14c05b72c73b49bc2d306e7c8b86223

$results = [
  'id' => 'abc123',
  'address.id' => 'def456',
  'address.coordinates.lat' => '12.345',
  'address.coordinates.lng' => '67.89',
  'address.coordinates.geo.accurate' => true,
];

function unflatten($data) {
  $output = [];
  foreach ($data as $key => $value) {
    $parts = explode('.', $key);
    $nested = &$output;
    while (count($parts) > 1) {
      $nested = &$nested[array_shift($parts)];
      if (!is_array($nested)) $nested = [];
    }
    $nested[array_shift($parts)] = $value;
  }
  return $output;
}

echo json_encode(unflatten($results));

/*
{
  "id": "abc123",
  "address": {
    "id": "def456",
    "coordinates": {
      "lat": "12.345",
      "lng": "67.89",
      "geo": {
        "accurate": true
      }
    }
  }
}
*/
Run Code Online (Sandbox Code Playgroud)

这稍微受到以下资源的影响: