我嘗試使用以下代碼合併MyData
中數組'c'和'a'中的數據,但結果仍然損壞。無法從cakephp2中的find('all')合併嵌套數組數據
我的代碼有問題嗎?或者我簡單地犯了一個錯誤,我如何合併數組?我正在做各種各樣的東西來解決問題,但找不到任何可行的解決方案。一些示例或提示將非常棒!
想合併[my_test]
和[my_date]
裏面[MyData]
。
Array
(
[0] => Array
(
[MyData] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
)
[c] => Array
(
[my_test] => math
)
[a] => Array
(
[my_date] => 2017-08-13
)
).....Loops
[1] => Array
(
我希望的結果是象
Array
(
[0] => Array
(
[MyData] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
[my_test] => math
[my_date] => 2017-08-13
我提出合併數組中並顯示爲上述碼的邏輯,但沒有能合併
$res = $this->find('all', $cond); // All the data are fetchd from this $res
$count = count($res);
for($i=0;$i<$count;$i++){
$result[] = $res[$i] ;
$fixed_arrays[] = $result[$i]['MyData'];
if (!empty($result[$i]['c'])) {
$corrupt_c_array = $result[$i]['c'];
$fixed_arrays = array_merge($fixed_arrays,$corrupt_c_array);
}
if(!empty($result[$i]['a'])) {
$corrupt_a_array = $result[$i]['a'];
$fixed_arrays = array_merge($fixed_arrays, $corrupt_a_array);
}
}
$result['data'] = $fixed_arrays; // This $result['data'] should show the expected result.
[更新] 聽說有關函數set :: combine for cakephp2,有沒有辦法使用set :: combine,因爲它的cakephp2?
如何將其他$數據合併到$ data ['MyData']? – Tony
它在臨時數組上創建一個變量'$ data ['MyData'] ['my_test']'和'$ data ['MyData'] ['my_date']',然後將其賦值給'$ fixed_arrays'列表 – Sundar