1
你好,我需要從其中包含兒童的數據給孩子的父母改變多維數組的結構,它包含有關父母信息多維數組,葉子作爲父鍵的鍵。遞歸迭代
$tab = [
'movies' => [
'action',
'drama',
'comedy' => [
'romance' => ['90th'],
'boring'
]
],
'colors' => [
'red'=>'light',
'green'=> [
'dark',
'light'
],
'oragne'
]
];
轉移到
$tab = [
'90th' => [
'romance' => [
'comedy' => 'movies'
]
],
'boring' => [
'comedy' => 'movies'
],
'comedy' => 'movies',
'drama' => 'movies',
'action' => 'movies',
'light' => [
'red',
'green' => 'colors'
],
'dark' => [
'green' => 'colors'
],
'oragne' => 'colors',
'green' => 'colors',
'red'
];
我知道獲得葉子我可以使用
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($tab), RecursiveIteratorIterator::LEAVES_ONLY) as $key => $value) {
$result[$value] = $key;
}
但它不工作,因爲我期望。
謝謝。我認爲這是一個普遍的問題,我可以用一種「簡單」的方式來做 – malzahar123