我有这个简单的数组:
[
'key1' => 'value',
'key2' => 'value',
'key3' => [
'u-key1' => 'value',
'u-key2' => 'value'
]
]
我想把它平展成这样:
[
'key1' => 'value',
'key2' => 'value',
'u-key1' => 'value',
'u-key2' => 'value'
]
我确信在Laravel中有一种简单的方法可以使用Collection/Arr helper来完成它,但不能弄清楚。
试试这个
$data = [
'key1' => 'value',
'key2' => 'value',
'key3' => [
'u-key1' => 'value',
'u-key2' => 'value'
]
];
$array = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
foreach ($array as $key => $value) {
$result[$key] = $value;
}
print_r($result);
输出:
Array
(
[key1] => value
[key2] => value
[u-key1] => value
[u-key2] => value
)
你的问题的答案演示下面
https://paiza.io/projects/F7G_b_S_skT916OeBrl2xw
试试这个:你的数组:
$arr= [
'key1' => 'value',
'key2' => 'value',
'key3' => [
'u-key1' => 'value',
'u-key2' => 'value'
]
]
你的功能:
$data = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach($data as $index=> $d) {
$r[$index]=$d;
}
print_r($r);
更多信息请访问https://www.php.net/manual/en/function.array-column.php