make an array into a variable?

Discussion in 'PHP' started by bobby9101, Dec 6, 2006.

  1. #1
    How can I separate all the contents of an array by a "," and then make that string a variable?
     
    bobby9101, Dec 6, 2006 IP
  2. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is this what you were looking for?

    http://ca.php.net/manual/en/function.implode.php
     
    rgchris, Dec 6, 2006 IP
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks, i will look it over.

    but to clear it up. I have:

    say "blue" "green" "orange" in an array

    I want: $variable to equal blue, green, orange
     
    bobby9101, Dec 6, 2006 IP
  4. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, that's pretty much what the function does.
     
    rgchris, Dec 6, 2006 IP
  5. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #5
    for some reason it won't work.
    i echoed the new varialbe. and it just displays: Array
     
    bobby9101, Dec 6, 2006 IP
  6. thedark

    thedark Well-Known Member

    Messages:
    1,346
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    168
    Digital Goods:
    1
    #6
    use the following code:

    $string="";
    $i=0;
    while($array[$i]!="") {
    $string .= $array[$i];
    $i++;
    }
     
    thedark, Dec 6, 2006 IP
  7. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Are you sure you're echoing the new variable instead of the array? The function outputs a string, not an array.
     
    rgchris, Dec 6, 2006 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    
    foreach($array as $v)
    {
       $string .= $v . ",";
    }
    
    PHP:
    that'll do it ..... $string now contains array1,array2,array2
     
    krakjoe, Dec 6, 2006 IP
  9. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #9
    No point is iterating through it, just use implode() like you suggested earlier..

    
    <?php
    
    	$colorArray = array("blue", "green", "red");
    	$colors = implode(",", $colorArray);
    	
    	// Outputs: blue,green,red
    	echo $colors;
    
    ?>
    
    PHP:
     
    CodyRo, Dec 6, 2006 IP
  10. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #10
    he said that didn't work for him already, so I thought Id present another way....
     
    krakjoe, Dec 6, 2006 IP
  11. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I've used the same function for the same purpose over and over. The only reason it wouldn't work would be an easy to fix oversight.
     
    rgchris, Dec 6, 2006 IP
  12. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #12
    thanks all...
    for some reason, I had saved it the correct way, then ctrl+z (accident) and i guess saved it again.
    so it didn't work.
     
    bobby9101, Dec 6, 2006 IP