我想转(在PHP中)类似
(["a"] => (
["x"] => "foo",
["y"] => "bar"),
["b"] => "moo",
["c"] => (
["w"] => (
["z"] => "cow" )
)
)
到
(["a.x"] => "foo",
["a.y"] => "bar",
["b"] => "moo",
["c.w.z"] => "cow")
我如何实现这一目标?
答案 0 :(得分:7)
您可以创建一个递归函数:
function flatten($arr, &$out, $prefix='') {
$prefix = $prefix ? $prefix . '.' : '';
foreach($arr as $k => $value) {
$key = $prefix . $k;
if(is_array($value)) {
flatten($value, $out, $key);
}
else {
$out[$key] = $value;
}
}
}
您可以将其用作
$out = array();
flatten($array, $out);
答案 1 :(得分:1)
你在这里有一些不错的东西:http://davidwalsh.name/flatten-nested-arrays-php