有大量的提示和代码示例使用点表示法访问PHP数组,但我想做一些相反的事情。我想采用像这样的多维数组:
$myArray = array(
'key1' => 'value1',
'key2' => array(
'subkey' => 'subkeyval'
),
'key3' => 'value3',
'key4' => array(
'subkey4' => array(
'subsubkey4' => 'subsubkeyval4',
'subsubkey5' => 'subsubkeyval5',
),
'subkey5' => 'subkeyval5'
)
);
并将其转换为此(可能通过一些递归函数):
$newArray = array(
'key1' => 'value1',
'key2.subkey' => 'subkeyval',
'key3' => 'value3',
'key4.subkey4.subsubkey4' => 'subsubkeyval4',
'key4.subkey5.subsubkey5' => 'subsubkeyval5',
'key4.subkey5' => 'subkeyval5'
);
答案 0 :(得分:67)
代码
$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($myArray));
$result = array();
foreach ($ritit as $leafValue) {
$keys = array();
foreach (range(0, $ritit->getDepth()) as $depth) {
$keys[] = $ritit->getSubIterator($depth)->key();
}
$result[ join('.', $keys) ] = $leafValue;
}
输出
Array
(
[key1] => value1
[key2.subkey] => subkeyval
[key3] => value3
[key4.subkey4.subsubkey4] => subsubkeyval4
[key4.subkey4.subsubkey5] => subsubkeyval5
[key4.subkey5] => subkeyval5
)
演示:http://codepad.org/YiygqxTM
我需要去,但如果你明天需要解释,请问我。
答案 1 :(得分:4)
这将处理任意级别的嵌套:
<? //PHP 5.4+
$dotFlatten = static function(array $item, $context = '') use (&$dotFlatten){
$retval = [];
foreach($item as $key => $value){
if (\is_array($value) === true){
foreach($dotFlatten($value, "$context$key.") as $iKey => $iValue){
$retval[$iKey] = $iValue;
}
} else {
$retval["$context$key"] = $value;
}
}
return $retval;
};
var_dump(
$dotFlatten(
[
'key1' => 'value1',
'key2' => [
'subkey' => 'subkeyval',
],
'key3' => 'value3',
'key4' => [
'subkey4' => [
'subsubkey4' => 'subsubkeyval4',
'subsubkey5' => 'subsubkeyval5',
],
'subkey5' => 'subkeyval5',
],
]
)
);
?>
答案 2 :(得分:3)
RecursiveIteratorIterator
已有答案。但这是更优化解决方案,避免使用嵌套循环:
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($arr),
RecursiveIteratorIterator::SELF_FIRST
);
$path = [];
$flatArray = [];
foreach ($iterator as $key => $value) {
$path[$iterator->getDepth()] = $key;
if (!is_array($value)) {
$flatArray[
implode('.', array_slice($path, 0, $iterator->getDepth() + 1))
] = $value;
}
}
这里有几点需要做。注意这里使用RecursiveIteratorIterator::SELF_FIRST
常量。重要的是,默认值为RecursiveIteratorIterator::LEAVES_ONLY
,不允许我们访问所有密钥。因此,通过这个常量集,我们从数组的顶层开始,然后更深入。这种方法允许我们使用RecursiveIteratorIterator::getDepth
方法存储密钥的历史记录并准备密钥。
答案 3 :(得分:1)
这是我对递归解决方案的看法,它适用于任何深度的数组:
function convertArray($arr, $narr = array(), $nkey = '') {
foreach ($arr as $key => $value) {
if (is_array($value)) {
$narr = array_merge($narr, convertArray($value, $narr, $nkey . $key . '.'));
} else {
$narr[$nkey . $key] = $value;
}
}
return $narr;
}
可以称为$newArray = convertArray($myArray)
。
答案 4 :(得分:1)
这种方法与上面的Blafrat类似 - 但只是将数组作为值处理。
function dot_flatten($input_arr, $return_arr = array(), $prev_key = '')
{
foreach ($input_arr as $key => $value)
{
$new_key = $prev_key . $key;
// check if it's associative array 99% good
if (is_array($value) && key($value) !==0 && key($value) !==null)
{
$return_arr = array_merge($return_arr, dot_flatten($value, $return_arr, $new_key . '.'));
}
else
{
$return_arr[$new_key] = $value;
}
}
return $return_arr;
}
(唯一不会发现的情况是你有一个关联的值,但第一个键是0。)
请注意,RecursiveIteratorIterator可能比常规递归函数慢。 https://xenforo.com/community/threads/php-spl-why-is-recursiveiteratoriterator-100x-slower-than-recursive-search.57572/
在这种情况下,使用为1000次迭代php5.6提供的样本数组,此代码的速度提高了两倍(递归= .032 vs interator = .062) - 但对于大多数情况,差异可能无关紧要。主要是我更喜欢递归,因为我发现Iterator的逻辑对于像这样的简单用例来说是不必要的复杂。
答案 5 :(得分:0)
你可以这样做,但克里斯的答案应该是首选:
<?php
$array = array();
foreach($myArray as $key=>$value){
//1st level
if(is_array($value)){
//2nd level
foreach($value as $key_b=>$value_b){
//3rd level
if(is_array($value_b)){
foreach($value_b as $key_c=>$value_c){
$array[$key.'.'.$key_b.'.'.$key_c]=$value_c;
}
}else{
$array[$key.'.'.$key_b]=$value_b;
}
}
}else{
$array[$key]=$value;
}
}
print_r($array);
/*
Array
(
[key1] => value1
[key2.subkey] => subkeyval
[key3] => value3
[key4.subkey4.subsubkey4] => subsubkeyval4
[key4.subkey4.subsubkey5] => subsubkeyval5
[key4.subkey5] => subkeyval5
)
*/