您好,我有数组这样的可变深度:如何格式化多维数组
$array = [ // depth of array and number of values are variable
'this' => [
'a' => [
'b' => 'some value'
],
'c' => [
'd' => 'some value'
],
],
'that' => [
'path' => [
'to' => [
'val' => 'some value'
],
]
]
];
,我需要有这样的:
$array['this.a.b'] => 'some value';
$array['this.c.d'] => 'some value';
$array['that.path.to.val'] => 'some value';
或本可以更好:
$array[
0 => [
'key' => 'this.a.b',
'val' => 'some value'
],
1 => [
'key' => 'this.c.d',
'val' => 'some value'
],
2 => [
'key' => 'that.path.to.val',
'val' => 'some value'
],
]
我需要从数组中获取数据,并将它们设置为如下形式:
<input name="this.a.b" value="some value">
我试过类似的东西,但效果不好。
private static function reformat($input, $k = '', &$keys = [])
{
foreach($input as $key => $val) {
$k .= '.' . $key;
if (is_array($val)){
static::reformat($val, $k, $keys);
}else{
$keys[$k] = $val;
}
}
return $keys;
}
感谢您的帮助
发布您的尝试。发布您的预期结果 –