根据数组键将Flat PHP Array转换为嵌套数组? [英] Convert Flat PHP Array to Nested Array based on Array Keys?

查看:71
本文介绍了根据数组键将Flat PHP Array转换为嵌套数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to convert a flat array where the array keys indicate the structure into a nested array where the parent element becomes element zero, i.e. in the example:

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

它需要转换为:

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

这是一个示例输入数组:

Here is an example input array:

$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' ),
  ),
);

我的头撞在墙上好几个小时了,仍然无法正常工作.我想我已经看了太久了.预先感谢.

I've banged my head against the wall for hours and still can't get it working. I think I've been looking at it too long. Thanks in advance.

P.S.它应该是完全可嵌套的,即它应该能够转换如下所示的键:

P.S. It should be fully nestable, i.e. it should be able to convert a key that looks like this:

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

P.P.S. @Joseph Silber有一个聪明的解决方案,但是不幸的是,使用eval()并不是一个选择,因为它是一个WordPress插件,并且WordPress社区正试图淘汰eval()的使用.

P.P.S. @Joseph Silber had a clever solution but unfortunately using eval() is not an option for this as it's a WordPress plugin and the WordPress community is trying to stamp out the use of eval().

推荐答案

下面是一些代码,用于处理您最初建议作为输出的内容.

Here is some code to handle what you had originally proposed as output.

/**
 * 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数组,并设置值.

Basically, split your array keys into a nice array of ancestor keys, then use a nice function to decent into the $neweducation array using those parents, and set the value.

如果您希望更新的帖子具有输出,请在"explode"行之后的foreach循环中添加此内容.

If you want the output that you have updated your post to have, add this in the foreach loop after the line with 'explode'.

$ancestors[] = 0;

这篇关于根据数组键将Flat PHP Array转换为嵌套数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆