Connecting Tech Pros Worldwide Forums | Help | Site Map

Foreach loop with Array Keys

The.Relinator@gmail.com
Guest
 
Posts: n/a
#1: Dec 11 '06
Hello, I am unable to get the following piece of code to work as
desired. The fields valuable seems to contain all values however the
vaules variable only contains the first assignied with $values = '\'' .
$arry[$keys[0]] . '\''; Any help regarding this issue would be kindly
appreciated, thank you.

$keys = array_keys($arry);
$fields = '`' . $keys[0] . '`';
$values = '\'' . $arry[$keys[0]] . '\'';
foreach($keys as $key)
{
if($key != $keys[0]) {
$fields .= ', `' . $key . '`';
$vaules .= ', \'' . $arry[$key] . '\'';
}
}


The.Relinator@gmail.com
Guest
 
Posts: n/a
#2: Dec 11 '06

re: Foreach loop with Array Keys


Never mind, I found the issue, it was just a small spelling mistake. I
checked it many times and didnt find it until right after I posed this!

Rik
Guest
 
Posts: n/a
#3: Dec 12 '06

re: Foreach loop with Array Keys


The.Relinator@gmail.com wrote:
Quote:
Never mind, I found the issue, it was just a small spelling mistake.
I checked it many times and didnt find it until right after I posed
this!
You can do this easier you know.

$keys = '`'.implode('`,`',array_keys($arry)).'`';
$value = "'".implode("', '",$arry)."'";

BTW, it looks like you're trying to build this for an insert query. If your
variables come from 'outside', be sure to validate them all first!
--
Rik Wasmus


Closed Thread