0

I'm trying to print each value in this array as a comma-delimited string.

$domainext = array("com","net","edu","uk","au","in","biz","ca","cc","cd","bz","by");

Here is how I'm doing it.

 foreach ($domainext as $ext){
     $ext = implode(', ', $domainext);
     echo $ext."<br>";
     echo "<br>";
 }

this is the output.

com, net, edu, uk, au, in, biz, ca, cc, cd, bz, by <br>

(however, there are as many lines as there are array values)

I have tried using explode(), and it returns "array" with an error above it.

5
  • you dont need $ext = implode(', ', $domainext); inside the loop. Commented Jun 12, 2014 at 17:09
  • Don't worry about asking basic questions, stack overflow isn't just for advanced questions. Commented Jun 12, 2014 at 17:09
  • You don't need that loop. Just implode once Commented Jun 12, 2014 at 17:09
  • I think he wants one domain text on each line due to wrapping each one with <br> Commented Jun 12, 2014 at 17:10
  • Why a downvote ? i don't think that's fair since he is stating it's a basic question Commented Jun 12, 2014 at 17:12

4 Answers 4

2

I think this is what you are looking for:

$domainext = array("com","net","edu","uk","au","in","biz","ca","cc","cd","bz","by");

print implode("\n", $domainext);

This gives:

com
net
edu
....

Oops... replace the "\n" by a br tag if you are printing to a web page.

print implode("<br/>", $domainext);
Sign up to request clarification or add additional context in comments.

2 Comments

As seen below, join works too. I believe it is an alias of implode.
Just FYI.... implode takes an array and converts to a string. explode takes a string and converts it into an array. The "glue" can be specified for both.
1

You dont need to implode or explode anything :), try this:

foreach ($domainext as $ext){
        echo $ext."<br>";
        echo "<br>";
    }

Comments

1

If you just want to output the items in your array on a different row, you cna use join

echo join("<br/>",$domainext);

As others have said, no loop necessary.

Comments

0

Simply:

echo join('<br>', $domainext);

will do the job if you want each domain suffix separated with HTML line breaks (the exact required output is unclear from your question). Join is an alias of implode, and preferable from its clearer meaning.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.