我需要将数组键指示结构的平面数组转换为父元素变为零元素的嵌套数组,例如:
$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' ),
),
);
我的头撞在墙上好几个小时了,仍然无法使它工作。我想我看得太久了。提前谢谢。
附言:它应该是完全可嵌套的,也就是说,它应该能够转换如下所示的密钥:
x[1][2][3][4][5][6]
p.p.S.@Joseph Silber有一个聪明的解决方案,但不幸的是,使用eval()
并不是一个选择,因为它是一个WordPress插件,WordPress社区正试图杜绝使用eval()
。
这里有一些代码来处理您最初提出的输出。
/**
* 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;
$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);
<?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);
$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);
更新:修改了解决方案以处理新的需求。更新:清理了变量名并添加了注释。(最后一次编辑,我保证:(
根据上面的第一个建议,我通过修改$祖先变量找到了一个适用于.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
)
)
)
如果您总是用[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);