我想(在PHP中)变成类似的东西
(["a"] => (
["x"] => "foo",
["y"] => "bar"),
["b"] => "moo",
["c"] => (
["w"] => (
["z"] => "cow" )
)
)
到
(["a.x"] => "foo",
["a.y"] => "bar",
["b"] => "moo",
["c.w.z"] => "cow")
我该如何实现?
你可以创建一个递归函数:
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);
你这里有一些好东西:http://davidwalsh.name/flatten-nested-arrays-php