note 75783 deleted from function.implode by felipe
| From: | felipe@php.net | Date: | Mon, 24 Dec 2007 02:13:17 +0000 |
| Subject: | note 75783 deleted from function.implode by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133937@lists.php.net to get a copy of this message | ||
Note Submitter: troy dot cregger at gmail dot com
----
Concerning the function by: darwinkid at gmail dot com... Nice one, I found it useful.
I made a modification to it though to handle array keys of the form:
array(
[key1] => one
[key2] => two
[key3] => three
)
It also discards empty elements in the array so if you have:
array(
[key1] => one
[key2] => two
[key3] => three
[key4] =>
[key5] => five
)
You'd get:
one,two,three,five
rather than:
one, two, three, ,five
If you supply ", " as glue.
Here's my code:
<?php
function my_implode_by_key($glue, $key, $pieces) {
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($pieces));
foreach($it as $el) {if(substr($it->key(), 0, 3) == $key && $it->current() !=
'') {$arr[] = $it->current();}}
return implode($glue, $arr);
}
?>