FANDOM


PHP's native implode function is quite useful in many cases, but there comes a time where you need to implode a specific key's value. Here is a function which will allow you to do just that.

The Code:Edit

<?php
function implode_by_key($glue, $keyname, $pieces) {
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($pieces));
$arr = array();
foreach($it AS $element) {if($it->key() == $keyname) {$arr[] = $it->current();}}
return implode($glue, $arr);
}
?>