2

I am trying to insert the contents of an array in to a string using PHP. My array ($array1) looks like this:

Array1  
    (  
       [0] => http://www.example.com/1  
       [1] => http://www.example.com/2  

    )

I want to insert both links in to a coma separated string, so I can then insert it in to a database field.

I tried this:

foreach ($array1 as $name => $value) {
          $string1 .= $value . ",";
          }
  echo $string1;

Which does work, but I am doing this twice in my code for another array that I also want in a separate string ($string2)

    Array2  
    (  
       [0] => http://www.example.com/3  
       [1] => http://www.example.com/4  
    )

When I echo $string1 I get the correct output
http://www.example.com/1,http://www.example.com/2

But $string2 becomes this:
http://www.example.com/1,http://www.example.com/2,http://www.example.com/3,http://www.example.com/4

This happens even if I use different variable names in the foreach loop above.

Someone else also suggested I try this:

$string1 = implode(',' , $array1);  

But I'm not getting any output.

Any help as to how to solve this, or any different approach is greatly appreciated!

2
  • To get ouptut, you need to echo, print, etc, $string1 (see my answer below). The code you show, just sets $string1 to a certain value, but it doesn't display that value. Commented Jul 21, 2010 at 2:30
  • Yes, I was echoing it but got no output because I was using the wrong variable name :/ I need to get off the computer for a while.Thanks! Commented Jul 21, 2010 at 2:32

3 Answers 3

2

There's a PHP function called implode for this exact purpose.

$csv = implode(',', $array);

echo $csv; //blah,blah,blah,blah
Sign up to request clarification or add additional context in comments.

2 Comments

Implode, mentioned in OP. Looks like he expected it to output something.
heh, this said explode before. Knew I should have kept quiet for 5 more minutes ;)
1

implode should work fine. It's won't give you any output unless you echo or otherwise output the result, of course.

4 Comments

I'm echoing of course after the implode, still no output though
argh just noticed why I got no output after implode. I was using the wrong variable name. Thanks!
Heh. Posting code helps... just make sure you read it while you're posting; you may find you don't need to ask anything after getting a fresh look at it. Sounds silly but it's happened to me more than once, now I just paste a scaled down copy into another buffer and take a fresh look at it.
Yes, this is a small script I'm writing to learn programming. Extracting the code from the rest of the script definitely helps. Also, getting up for a while from the computer helps too hehe. Thanks!
0

$array1 = array("http://www.example.com/1", "http://www.example.com/2");
$array2 = array("http://www.example.com/3", "http://www.example.com/4");
echo implode(", ", array_merge($array1,$array2));

Output:

http://www.example.com/1, http://www.example.com/2, http://www.example.com/3, http://www.example.com/4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.