I'm trying to build a SQL query by looping in PHP using 2 arrays (one of which is array of arrays):
//build array of arrays using predefined arrays
$regions = array_filter(array($EAPRO, $WCARO, $ROSA, $TACRO, $MENA, $ESARO));
//just a normal array
$regionnames = array('EAPRO', 'WCARO', 'ROSA', 'TACRO', 'MENA', 'ESARO');
$sql = "";
foreach(array_combine($regions, $regionnames) as $region => $regionname)
{
$sql .="UPDATE `database`.`table` SET `region`='$regionname'
WHERE `countryname` IN (" . implode(",",$region) . ");";
}
echo $sql;
However, debugging this in ideone gives me:
Warning: implode(): Invalid arguments passed on line:
UPDATE `database`.`table` SET `region`='ESARO' WHERE `countryname` IN ();
Which tells me that the array on each loop is not being imploded correctly. Is there something wrong with the way I've defined my array of arrays?
Thanks
$region
should be defined by the loop:foreach(array_combine($regions, $regionnames) as $region => $regionname)
so on any given loop $region should be set to one of the arrays defined in$regions
no? – Gideon Oct 21 '12 at 22:52