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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Help adding values to elements in an array please?

    Hello All,

    Wondering if you can help please. If I have an array:

    PHP Code:
    print_r($array); 
    Which outputs like

    Code:
    one,two,three,four,five
    I wanted to dynamically add quote marks around each element in the $array like the below, how could I do it?

    Code:
    "one","two","three","four","five"
    Thank you in advance
    Last edited by stfc_boy; 09-15-2011 at 10:40 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    This example is for if you do make the array yourself
    PHP Code:
    $array = array("one","two","three");
    foreach(
    $array as $key => $value)
    {
         
    $array[$key] = '"'.$value.'"';
    }

    print_r($array); 
    If you can make the array yourself:
    PHP Code:
    $array = array('"one"','"two"','"three"');

    print_r($array); 

  • Users who have thanked Wanna for this post:

    stfc_boy (09-15-2011)

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts
    Thanks for your help Wanna,

    But unfortunately i'm calling this data in from a database which is then being held in an array rather than creating the array myself, so it is returned without quotes. I'm therefore still not too sure how I could reach my goal with quotes wrapped around each element in the array.

    Thanks
    Last edited by stfc_boy; 09-15-2011 at 10:33 AM.

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts
    Hi Wanna,

    Had a think again and used a foreach loop as you mentioned in your first example and got it working.

    Thanks for your help again.
    Last edited by stfc_boy; 09-15-2011 at 10:42 AM.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Im sorry for my mistake.

    The first code i posted i for an array you dont make youself.

    PHP Code:
    $array = array("one","two","three"); 
    foreach(
    $array as $key => $value

         
    $array[$key] = '"'.$value.'"'


    print_r($array); 


  •  

    Posting Permissions

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