PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Aug 08, 2020 5:50 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Mon Aug 01, 2005 3:49 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
Someone on a different forum I frequent asked for some code to create a comma seperated list of the values in an associated array, without using the empty entries.

The obvious solution that was first suggested was to iterate around a for..next loop checking each of the array values and concatenating it into a string. But I came up with this rather elegant solution:

Syntax: [ Download ] [ Hide ]
<?php

function not_empty($e){ return !empty($e); }

$string = implode(",",array_filter($array,"not_empty"));

?>


Just thought I'd share. I'd prefer to define not_empty as an anonymous function, but array_filter doesn't like using them as a callback for some reason.

NB: What's up with the tags? They're not working.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 5:47 pm 
Offline
DevNet Master

Joined: Thu Jan 30, 2003 9:26 pm
Posts: 2893
Location: Glasgow, Scotland
I'm working on it - should be restored in an hour or two.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group