0

我有以下代码,但它一次运行一个 foreach 循环。有没有办法让它们同时运行?

foreach($json['orders']['cnr_output_ship_to_header'] as $header)
{
    $orders_array[] = $header;
    $guests_array[] = $header['guests']['cnr_output_guest_detail'];
    $items_array[] = $header['items']['cnr_output_item_detail'];
}   

foreach($guests_array as $guests)
{
    pdo_insert('cnr_output_guest_detail', (array)$guests);
}
foreach($items_array as $items)
{
    pdo_insert('cnr_output_item_detail', (array)$items);
}
foreach($orders_array as $orders)
{
    pdo_insert('cnr_output_ship_to_header', (array)$orders);
}
4

2 回答 2

2

这应该工作得很好

foreach ( $json['orders']['cnr_output_ship_to_header'] as $header ) {
    pdo_insert('cnr_output_guest_detail', (array)  $header['guests']['cnr_output_guest_detail']);
    pdo_insert('cnr_output_item_detail', (array) $header['items']['cnr_output_item_detail']);
    pdo_insert('cnr_output_ship_to_header', (array) $header);
}
于 2012-12-19T19:07:07.810 回答
1

为什么不这样:

foreach($json['orders']['cnr_output_ship_to_header'] as $header) {
    //$orders_array[] = $header;
    pdo_insert('cnr_output_ship_to_header', (array)$header);
    //$guests_array[] = $header['guests']['cnr_output_guest_detail'];
    pdo_insert('cnr_output_guest_detail', (array)$header['guests']['cnr_output_guest_detail']);
    //$items_array[] = $header['items']['cnr_output_item_detail'];
    pdo_insert('cnr_output_item_detail', (array)$header['items']['cnr_output_item_detail']);

}
于 2012-12-19T19:07:36.250 回答