Foreach loop with Array Keys

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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] . ''';

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!

Re: Foreach loop with Array Keys wrote:
Quoted text here. Click to load it

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

Site Timeline