Adding elements to multidimensional array
我有一个问题,也就是说,我不知道如何使用foreach循环中的array_push()函数将新数据放入我的数组(读取数据库数据)。代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $result = array(); $i = 0; #$rows - data from the database foreach($res as $rows){ $result[$i] = ['aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff']; array_push($result[$i], ['gg' => 'hh', 'ii' => 'jj']); $i++; } #The expected result: #Array('aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff', 'gg' => 'hh', 'ii' => 'jj'); #Reality: #Array(0 => ['gg' => 'hh', 'ii' => 'jj'], 'aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff'); |
提前感谢您的帮助。
你必须这样做:
1 2 3 4 5 6 | <?php $result[] = ['aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff']; $result = array_merge($result[0], ['gg' => 'hh', 'ii' => 'jj']); print_r($result); |
网址:https://eval.in/850034