If I create the following 2 dimensional associative array:-
$insert[tab2][fields1] = value1;
$insert[tab1][fields2] = value2;
$insert[tab2][fields5] = value3;
$insert[tab1][fields7] = value4;
how do I do 2 levels of nested loop with the first level looping through the
first level of the array
You've created a 2x2 array, not a 1x4, which might be confusing you:
tab2 [fields1] [fields5]
tab1 [fields2] [fields7]
You almost have it, you just need to set your loop up to handle
unlimited field/value pairs in your array.
One option is to structure your loop like this:
foreach ($insert as
Ok - looking at what you did I cant see any diference apart from turning a
serious updated into a single update. This is usefull if I want to do an update
but I actualy want an insert (the update was just used for this example).
I actualy want to do an insert so i'me thinking the following
A, I was wondering why your variable was named insert.
Anyway, that's easy too.
foreach($insert as $table = $fields) {
$fieldList = array_keys($fields);
$fieldNames = implode(',', $fieldList);
$fieldValues = ''.implode(',', $fields).'';
echo 'insert into '.$table.' ('.$fieldNames.') values
Brent Baisley wrote:
foreach($insert as $table = $fields) {
$fieldList = array_keys($fields);
$fieldNames = implode(',', $fieldList);
$fieldValues = ''.implode(',', $fields).'';
echo 'insert into '.$table.' ('.$fieldNames.') values
('.$fieldValues.')';
}
That puts
5 matches
Mail list logo