基于数组键将Flat PHP数组转换为嵌套数组?

时间:2011-08-19 03:59:24

标签: php multidimensional-array

我需要将数组键指示结构的平面数组转换为嵌套数组,其中父元素变为元素零,即在示例中:

$education['x[1]'] = 'Georgia Tech';

需要转换为:

$education[1][0] = 'Georgia Tech';

这是一个示例输入数组:

$education = array(
  'x[1]'     => 'Georgia Tech',
  'x[1][1]'  => 'Mechanical Engineering',
  'x[1][2]'  => 'Computer Science',
  'x[2]'     => 'Agnes Scott',
  'x[2][1]'  => 'Religious History',
  'x[2][2]'  => 'Women\'s Studies',
  'x[3]'     => 'Georgia State',
  'x[3][1]'  => 'Business Administration',
);

这是输出应该是什么:

$education => array(
  1 => array(
    0 => 'Georgia Tech',
    1 => array( 0 => 'Mechanical Engineering' ),
    2 => array( 0 => 'Computer Science' ),
  ),
  2 => array(
    0 => 'Agnes Scott',
    1 => array( 0 => 'Religious History' ),
    2 => array( 0 => 'Women\'s Studies' ),
  ),
  3 => array(
    0 => 'Georgia State',
    1 => array( 0 => 'Business Administration' ),
  ),
);

我把头撞在墙上好几个小时仍然无法让它工作。我想我已经看了太久了。提前谢谢。

P.S。它应该是完全可嵌套的,即它应该能够转换看起来像这样的键:

x[1][2][3][4][5][6] 

P.P.S。 @Joseph Silber有一个聪明的解决方案,但不幸的是使用eval()不是一个选项,因为它是一个WordPress插件,而WordPress社区正试图消除eval()的使用。

6 个答案:

答案 0 :(得分:4)

这是一些代码来处理你最初提出的输出。

/**
 * Give it and array, and an array of parents, it will decent into the
 * nested arrays and set the value.
 */
function set_nested_value(array &$arr, array $ancestors, $value) {
  $current = &$arr;
  foreach ($ancestors as $key) {

    // To handle the original input, if an item is not an array, 
    // replace it with an array with the value as the first item.
    if (!is_array($current)) {
      $current = array( $current);
    }

    if (!array_key_exists($key, $current)) {
      $current[$key] = array();
    }
    $current = &$current[$key];
  }

  $current = $value;
}


$education = array(
  'x[1]'     => 'Georgia Tech',
  'x[1][1]'  => 'Mechanical Engineering',
  'x[1][2]'  => 'Computer Science',
  'x[2]'     => 'Agnes Scott',
  'x[2][1]'  => 'Religious History',
  'x[2][2]'  => 'Women\'s Studies',
  'x[3]'     => 'Georgia State',
  'x[3][1]'  => 'Business Administration',
);

$neweducation = array();

foreach ($education as $path => $value) {
  $ancestors = explode('][', substr($path, 2, -1));
  set_nested_value($neweducation, $ancestors, $value);
}

基本上,将数组键拆分为一个很好的祖先键数组,然后使用一个很好的函数来使用这些父项进入$ neweducation数组,并设置值。

如果您想要更新帖子的输出,请在“爆炸”行后面的foreach循环中添加。

$ancestors[] = 0;

答案 1 :(得分:3)

$result = array();

foreach( $education as $path => $value ) {

    $parts = explode('][', trim( $path, 'x[]' ) );
    $target =& $result;

    foreach( $parts as $part )
        $target =& $target[$part];

    $target = array($value);
}

var_dump($result);

答案 2 :(得分:2)

<?php
$education = array(
  'x[1]'     => 'Georgia Tech',
  'x[1][1]'  => 'Mechanical Engineering',
  'x[1][2]'  => 'Computer Science',
  'x[2]'     => 'Agnes Scott',
  'x[2][1]'  => 'Religious History',
  'x[2][2]'  => 'Women\'s Studies',
  'x[3]'     => 'Georgia State',
  'x[3][1]'  => 'Business Administration',
);
$x = array();
foreach ($education as $key => $value) {
        parse_str($key . '[0]=' . urlencode($value));
}
var_dump($x);

答案 3 :(得分:1)

$education = array(
  'x[1]'     => 'Georgia Tech',
  'x[1][1]'  => 'Mechanical Engineering',
  'x[1][2]'  => 'Computer Science',
  'x[2]'     => 'Agnes Scott',
  'x[2][1]'  => 'Religious History',
  'x[2][2]'  => 'Women\'s Studies',
  'x[3]'     => 'Georgia State',
  'x[3][1]'  => 'Business Administration',
  // Uncomment to test deep nesting.
  // 'x[1][2][3][4][5][6] ' => 'Underwater Basket Weaving',
);

$newarray = array();
foreach ($education as $key => $value) {

  // Parse out the parts of the key and convert them to integers.
  $parts = explode('[', $key);
  for($i = 1; $i < count($parts); $i += 1) {
    $parts[$i] = intval(substr($parts[$i], 0, 1));
  }

  // Walk the parts, creating subarrays as we go.
  $node = &$new_array;
  for($i = 1; $i < count($parts); $i += 1) {
    // Create subarray if it doesn't exist.
    if (!isset($node[$parts[$i]])) {
      $node[$parts[$i]] = array();
    }
    // Step down to the next dimension.
    $node = &$node[$parts[$i]];
  }
  // Insert value.
  $node[0] = $value;
}
$education = $new_array;

var_dump($education);

更新:修改后的解决方案来处理新要求。 更新:清理变量名称并添加注释。 (最后编辑,我保证:))

答案 4 :(得分:0)

如果您始终使用[0]将第一个元素存储在数组中,那么您可以使用它:

$education = array(
    'x[1][0]' => 'Georgia Tech',
    'x[1][1]' => 'Mechanical Engineering',
    'x[1][2]' => 'Computer Science',
    'x[2][0]' => 'Agnes Scott',
    'x[2][1]' => 'Religious History',
    'x[2][2]' => 'Women\'s Studies',
    'x[3][0]' => 'Georgia State',
    'x[3][1]' => 'Business Administration'
);

$x = array();

foreach ($education as $key => $val)
{
    eval('$'.$key.'=$val;');
}

print_r($x);

答案 5 :(得分:0)

基于第一个suggestion above,我通过修改$ ancestors变量找到了适用于我的.ini文件的解决方案。

编辑:以下是我的工作代码的完整版本:https://stackoverflow.com/a/38480646/1215633

//$ancestors = explode('][', substr($path, 2, -1));
$ancestors = explode('.', $path);

我在数组中进行了此设置,基于.ini文件:

[resources.db.adapter] => PDO_MYSQL
[resources.db.params.host] => localhost
[resources.db.params.dbname] => qwer
[resources.db.params.username] => asdf
[resources.db.params.password] => zxcv
[resources.db.params.charset] => utf8
[externaldb.adapter] => PDO_MYSQL
[externaldb.params.host] => localhost
[externaldb.params.dbname] => tyui
[externaldb.params.username] => ghjk
[externaldb.params.password] => vbnm
[externaldb.params.charset] => latin1

结果如预期:

Array
(
[resources] => Array
    (
        [db] => Array
            (
                [adapter] => PDO_MYSQL
                [params] => Array
                    (
                        [host] => localhost
                        [dbname] => qwer
                        [username] => asdf
                        [password] => zxcv
                        [charset] => utf8
                    )

            )

    )

[externaldb] => Array
    (
        [adapter] => PDO_MYSQL
        [params] => Array
            (
                [host] => localhost
                [dbname] => tyui
                [username] => ghjk
                [password] => vbnm
                [charset] => latin1
            )

    )

)