jj20051 Posted August 20, 2010 Share Posted August 20, 2010 I have a foreach() that I want to run and I want to check before I test it out if this is even possible or how I would do this correctly if it isn't. I have 3 arrays that all pull data from a database earlier in the script. These three all "match" so that they all belong to the same product, but they are from different queries. I want a foreach() to extract data from all 3 of them and insert that into a database until the arrays run out of data. foreach (($titles_list as $title) && ($prices_list as $price) && ($services_list as $service_id)) { mysql_query("INSERT INTO product_invoices (name, invoice_id, price, quantity, date_time, service_id) VALUES('$title', '$invoice_id', '$price', '$today_slashes', '$service_id')") or die(mysql_error()); } Hopefully you see where I'm going with this. Link to comment https://forums.phpfreaks.com/topic/211241-is-this-foreach-correctpossible/ Share on other sites More sharing options...
trq Posted August 20, 2010 Share Posted August 20, 2010 That won't work. You'd need to use a counter and access each index that way. Link to comment https://forums.phpfreaks.com/topic/211241-is-this-foreach-correctpossible/#findComment-1101490 Share on other sites More sharing options...
jj20051 Posted August 20, 2010 Author Share Posted August 20, 2010 An example would be nice, but good to know I'm wrong. Link to comment https://forums.phpfreaks.com/topic/211241-is-this-foreach-correctpossible/#findComment-1101492 Share on other sites More sharing options...
sasa Posted August 20, 2010 Share Posted August 20, 2010 <?php $titles_list = array(1,2,3); $prices_list = array(11,22,33); $services_list = array(111,222,333); reset($titles_list); reset($prices_list); reset($services_list); $title = current($titles_list); $price = current($prices_list); $service_id = current($services_list); while ($title and $price and $service_id){ mysql_query("INSERT INTO product_invoices (name, invoice_id, price, quantity, date_time, service_id) VALUES('$title', '$invoice_id', '$price', '$today_slashes', '$service_id')") or die(mysql_error()); $title = next($titles_list); $price = next($prices_list); $service_id = next($services_list); } ?> Link to comment https://forums.phpfreaks.com/topic/211241-is-this-foreach-correctpossible/#findComment-1101507 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.