Foreach loop with Array Keys

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The.Relinator@gmail.com

    Foreach loop with Array Keys

    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($arr y);
    $fields = '`' . $keys[0] . '`';
    $values = '\'' . $arry[$keys[0]] . '\'';
    foreach($keys as $key)
    {
    if($key != $keys[0]) {
    $fields .= ', `' . $key . '`';
    $vaules .= ', \'' . $arry[$key] . '\'';
    }
    }

  • The.Relinator@gmail.com

    #2
    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!

    Comment

    • Rik

      #3
      Re: Foreach loop with Array Keys

      The.Relinator@g mail.com wrote:
      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


      Comment

      Working...