下面是一個例子數組:PHP將嵌套數組轉換爲單個數組,同時連接鍵?
$foo = array(
'employer' => array(
'name' => 'Foobar Inc',
'phone' => '555-555-5555'
),
'employee' => array(
'name' => 'John Doe',
'phone' => '555-555-5556',
'address' => array(
'state' => 'California',
'zip' => '90210'
)
),
'modified' => '2009-12-01',
);
而且我想獲得這樣的結果:
$fooCompressed = array(
'employer_name' => 'Foobar Inc',
'employer_phone' => '555-555-5555',
'employee_name' => 'John Doe',
'employee_phone' => '555-555-5556'
'employee_address_state' => 'California',
'employee_address_zip' => '90210',
'modified' => '2009-12-01'
)
我將如何去寫一個遞歸函數來處理呢?
+1這是非常接近我會怎麼做。因爲鍵正在被修改,所以沒有內置的函數可以爲你做,而且你肯定需要遞歸來深入查看也是數組的任何子值。 – zombat
很好的例子。我喜歡通過引用傳遞輸出數組的想法。 – GSto