如何动态获取多维数组?

时间:2014-01-30 13:47:11

标签: php multidimensional-array

我的代码只填充数组$ workshops

中的最后一个数组

我的循环函数填充数组$ workshop,就像这样

    while (ECSE_purchase::have_products()) {
                ECSE_purchase::the_product();
                $prodid = ECSE_purchase_product::get_the_ID();
                $workshop_data = ETICKETDATA::get_workshop_details_helper($prodid);
                $workshops = array();  
 if($prodid == 565) {
............
} else { 
$workshopsTmp = array();
                            $workshopsTmp['workshop_barcode'] = $data['workshop_barcode'];
                            $workshopsTmp['workshop_id'] = ECSE_purchase_product::get_the_ID();
                            $workshopsTmp['workshop_title'] = ECSE_purchase_product::get_the_name();
                            $workshopsTmp['workshop_time'] = $workshop_data['time'];
                            $workshopsTmp['workshop_room'] = $workshop_data['room'];
                            $workshopsTmp['workshop_num_of_tickets'] = ECSE_purchase_product::get_the_QTY();
                            $workshops[] = $workshopsTmp;
}

但在这里我只是得到了最后一个数组而不是这样:

$workshops = array(array('workshop_barcode' => '0101010101',
                                'workshop_id' => '589',
                                'workshop_title' => 'Swimming',
                                'workshop_time' => '12:00',
                                'workshop_room' => 'Room 1',
                                'workshop_num_of_tickets' => '2'),
                            array('workshop_barcode' => '03030303003',
                                'workshop_id' => '568',
                                'workshop_title' => 'Running',
                                'workshop_time' => '15:00',
                                'workshop_room' => 'Room 3',
                                'workshop_num_of_tickets' => '3'),
                            array('workshop_barcode' => '0505050505',
                                'workshop_id' => '570',
                                'workshop_title' => 'Biking',
                                'workshop_time' => '16:00',
                                'workshop_room' => 'Room 2',
                                'workshop_num_of_tickets' => '2'));

任何指示赞赏。

的问候,

只需编辑循环

1 个答案:

答案 0 :(得分:1)

您不断重新定义初始数组。在while循环之前尝试放置$ workshop数组

$workshops = array();  

while (ECSE_purchase::have_products()) {
                ECSE_purchase::the_product();
                $prodid = ECSE_purchase_product::get_the_ID();
                $workshop_data = ETICKETDATA::get_workshop_details_helper($prodid);

 if($prodid == 565) {
............
} else { 
$workshopsTmp = array();
                            $workshopsTmp['workshop_barcode'] = $data['workshop_barcode'];
                            $workshopsTmp['workshop_id'] = ECSE_purchase_product::get_the_ID();
                            $workshopsTmp['workshop_title'] = ECSE_purchase_product::get_the_name();
                            $workshopsTmp['workshop_time'] = $workshop_data['time'];
                            $workshopsTmp['workshop_room'] = $workshop_data['room'];
                            $workshopsTmp['workshop_num_of_tickets'] = ECSE_purchase_product::get_the_QTY();
                            $workshops[] = $workshopsTmp;
}