SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Array question

  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have an array:

    $form[name] = "joe"
    $form[address] = "blah"
    $form[city] = "blah"

    how can i put the key=>value of $form into a new variable as a string?

    $new_var = "name:joe address:blah city:blah"

  2. #2
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    serialize does something like that.. or do you want the $new_var to be in a specific format, like you had below, where the key/value are separated by a colon and the fields are separated by spaces?

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do it this way



    //Takes an array and creates key/value pairs
    //based off the $delim agrument then returns the string
    function strFromArray($array, $delim) {
    foreach($array as $key => $val) {
    $tmp .= "$key$delim$val ";
    }
    return $tmp;
    }


    $form[name] = "joe";
    $form[address] = "blah";
    $form[city] = "blah";

    //Sample Usage
    print strFromArray($form, ":");
    Last edited by freddydoesphp; May 14, 2001 at 16:15.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What way?

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh... I see you must be using Netscape let me try and fix it so vb will let you see what I typed in in my previous post. Or you can view it in IE
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry about that. I see it now.
    Thanks for your help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •