这样做的速度更快?

时间:2012-12-19 19:01:48

标签: php arrays foreach

我有以下代码,但它一次运行一个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);
}

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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']);

}