您好,我有數組這樣的可變深度:如何格式化多維數組
$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;
}
感謝您的幫助
發佈您的嘗試。發佈您的預期結果 –