0

I have the following code, but it runs the foreach loop one at a time. Is there a way of making them all run at once?

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);
}
Alister Bulman
35.3k9 gold badges75 silver badges113 bronze badges
asked Dec 19, 2012 at 19:01
5
  • 4
    The indentation is deceptive. There is no nesting happening here, just a set of foreach loops arranged in series. Commented Dec 19, 2012 at 19:05
  • Yup,There is no nesting, the indentation helps me group things quickly while coding. Since my coding is very messy. How would I nest these to do what I described? Commented Dec 19, 2012 at 19:09
  • How long does it take to execute now? How long do you need it to execute in? "Faster" is meaningless without specific goals. Commented Dec 19, 2012 at 19:13
  • I'm going through thousands if not millions of keys and values, every millisecond counts in the long run. I'll take any suggestions that improve the speed, if it's proven to execute faster. Commented Dec 19, 2012 at 19:21
  • 1
    Are you sure this is the slowest part of your code? If you're inserting multiple rows, use a multiple insert query (dev.mysql.com/doc/refman/5.6/en/insert-speed.html). Commented Dec 19, 2012 at 19:31

2 Answers 2

2

This should work just fine

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);
}
answered Dec 19, 2012 at 19:07
Sign up to request clarification or add additional context in comments.

Comments

1

why not this:

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']);
}
answered Dec 19, 2012 at 19:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.