Jump to content

Implode an array


Peter Knight
 Share

Recommended Posts

I have two sets of tags and I'm manually adding a comma after the echo which works but adds a comma to the very last tag too.

{echo "<i class=\"fa fa-globe\"></i>";
foreach ($update->client_location as $geo) 
{echo "{$geo->title},";}
										
										
{echo "<i class=\"fa fa-tags\"></i>";
foreach ($update->blog_tags as $tags) 
{echo "{$tags->title},";}

post-1166-0-82691200-1416429490_thumb.pn

You can see in the above screen grab that this works but adds a comma to the last word too. It's not a huge deal but I'd prefer the last word not to have a comma.

I think it's called "implode" in PHP when you take an array and automagically separate each value with a comma?

Would I end up with exactly the same issue by using an implode function?

Link to comment
Share on other sites

If you want a more idiotic way of doing it, you could add the comma with CSS and just not do it for the last child......

(PS: Idiotic because it separates out the content into two places, but worth mentioning.)

You could also identify the last iteration and not add a comma for it.

So, if NOT last, then add comma, would be the way....

PPS: Going by the questions, does this mean you got some work??? :)

Link to comment
Share on other sites

I prefer to do it this way: this, another, that & those

$out = '';
$count = count($update->client_location);

foreach ($update->client_location as $key => $geo) {
	$div = ($key + 1 === $count) ? ($count === 1 ? '' : ' & ') : ($key === 0 ? '' : ', ');
	$out .= $div . $geo->title;
}
 
echo $out;
  • Like 5
Link to comment
Share on other sites

I'm actually redoing my own site Joss :)

Ah, filling in .....

Martijn, the more I look at your solution, the cleverer it gets :)

It is also one of those that I would put a big comment above because when I look at it again in a year's time I would be thinking, "Now, what the hell was I trying to do there?" 

  • Like 1
Link to comment
Share on other sites

I prefer to do it this way: this, another, that & those

$out = '';
$count = count($update->client_location);

foreach ($update->client_location as $key => $geo) {
	$div = ($key + 1 === $count) ? ($count === 1 ? '' : ' & ') : ($key === 0 ? '' : ', ');
	$out .= $div . $geo->title;
}
 
echo $out;

Thanks Martijn

That's pretty cool. I can kind of make out whats happening there. Love the ampersand at the end too.

Link to comment
Share on other sites

  • 9 months later...

You can also PW API since 2.4 or 2.5

echo $page->blog_tags->implode(", ", "title");

I really like the simpler markup here and I'm using it to echo a series of blog categories separate by a ,

<?php 
$page->blog_category->implode(", ", "title");
?>

Im unsure though how to wrap the title in a link.

I'm thinking I first need to create a variable called $cats and associate it with my blog_category field.

I then need to place an anchor before the title.

<?php 
$cats = $page->blog_category;
echo 
$page->blog_category->implode(", ", "<a href=\"$cats->url\">title</a>");
?>

As this produces no output and no error, I'm clearly doing something wrong.

Looking at Ryans example of the new API, I can modify an example to include the url

<?php 
$cats = $page->blog_category; 
echo $cats->implode(function($cat) {
return "<a href='$cat->url'>$cat->title</a>";
});  
?> 

I know I'm close! Any help much appreciated.

Link to comment
Share on other sites

You've two options here: using string-tag-replacement or a function like you tried

<?php 
$cats = $page->blog_category; 

echo $cats->implode(", ", "<a href='{url}'>{title}</a>");

// or (notice the first implode argument)
echo $cats->implode(", ", function($cat) {
  return "<a href='$cat->url'>$cat->title</a>";
});

Detailed instructions about how these api functions work can be found here: https://processwire.com/talk/topic/5098-new-wirearray-api-additions-on-dev/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...