Reputation: 1553
I want to organize my URLS so they are like this
url1.com
:url2.com
:url3.com
How can i check if its the last entry in the array to make sure it dosen't put a new line and a : after the last URL ?
foreach($urls as $url) {
$splits .= "$url\n:";
}
Upvotes: 0
Views: 61
Reputation: 4062
If your problem is as easy as mentioned then you can use the implode
method (as others solutions show). For more complex constructs you may want to use something simmilar to this:
foreach ($urls as $url) {
if (!end($urls) === $url) {
// the n-th element
// ...
}
// the last element
// ...
}
Upvotes: 0
Reputation: 7005
$string = implode(PHP_EOL.':', $urls);
You should use the PHP_EOL constant so you are not depending on a specific OS.
Upvotes: 3