Re: 2 dimensional array processing
| From: | Brent Baisley | Date: | Tue, 11 Jan 2005 15:02:57 +0000 |
| Subject: | Re: 2 dimensional array processing | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-206075@lists.php.net to get a copy of this message | ||
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 $table => $fields) {
foreach($fields as $field => $value) {
$fieldValuePairs[] = "$field = $value";
}
$fieldValueStr = implode(',', $fieldValuePairs);
echo "update $table set $fieldValueStr";
unset($fieldValuePairs);
}
On Jan 11, 2005, at 6:22 AM, Benjamin Edwards wrote:
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 and the second nesting through the second. something like:- foreach( $insert as $table => $fields ) { foreatch( $fields as $field => $value ) {echo "update $table set $field = $value";} } Regards, Ben -- Brent Baisley Systems Architect Landover Associates, Inc. Search & Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577