Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Array to string

  1. #1
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts

    Array to string

    How would I go about turning

    Array
    (
    [Screen] => 1
    [Door knob] => 2
    [Bulb] => 3
    )

    into

    Screen - 1
    Door knob - 2
    Bulb - 3


    Thanks!
    Last edited by seco; 07-05-2013 at 12:09 AM.

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Try

    PHP Code:
    $keys array_keys($myArray);
    $i 0;
    while( 
    $i count($keys) )
    {
    echo 
    $keys[$i] . "-" $myArray[$i];
    $i++;

    where $myArray is the original array.

    Edit: Had a typo where I didn't put quotes around the dash, making it a minus sign and possibly leading to some hideous confusion in your script :P


    HTH
    Dan
    Last edited by whizard; 07-04-2013 at 11:23 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    ty for the fast reply. so im getting the names but not the numbers.

    PHP Code:
    $myArray $_POST['wo_stocks'];

    $keys array_keys($myArray);
    $i 0;
    while( 
    $i count($keys) )
    {
    echo 
    $keys[$i] . "-" $myArray[$i];
    $i++;

    im pulling the array from a form using name="wo_stocks[$number']" which posts as

    PHP Code:
    Array
    (
    [
    Screen] => 1
    [Door knob] => 2
    [Bulb] => 3

    what am i doing wrong here..

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    So the final output is:

    Screen -
    Door Knob -
    Bulb -
    ?

    Can you elaborate on the form thing again, or post the relevant code of the form?

    Thanks
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    Yes that is the final output.

    I have a form with multiple of the same field because of how i need to enter it into a db.

    this is within php and posts just fine.
    Code:
    <input type='text' name='wo_stocks[$tag]' value='1' />
    <input type='text' name='wo_stocks[$tag]' value='2' />
    <input type='text' name='wo_stocks[$tag]' value='3' />
    $tag is the name, Bulb, screen, etc..

    which outputs the array.

    the posts output in firebug show as

    PHP Code:
    wo_stocks[Screen]:1
    wo_stocks
    [Door knob]:2
    wo_stocks
    [Bulb]:
    Last edited by seco; 07-04-2013 at 11:55 PM.

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    That makes no sense. What about this:

    PHP Code:
    echo $keys[$i] . "-" $myArray[ . "'" $keys[$i] . "'" . ]; 
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • Users who have thanked whizard for this post:

    seco (07-05-2013)

  • #7
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    I got a error with that but removed all the quote to this

    PHP Code:
    echo $keys[$i] . "-" $myArray[$keys[$i]]; 
    and it worked!

    Screen-1
    Door knob-2
    Bulb-3

    thank you very much!

    ps: nice avatar.

  • #8
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Ah...my bad on quotes..I'm rusty :P

    Glad you got it, happy 4th!

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

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