note 74011 deleted from function.implode by hholzgra

From: Date: Thu, 02 Aug 2012 13:18:12 +0000
Subject: note 74011 deleted from function.implode by hholzgra
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189740@lists.php.net to get a copy of this message
Note Submitter: darwinkid at gmail dot com ---- this is a little function i made to implode an array based on key. its main purpose is to implode elements within a multi-dimensional array. its slightly different than some of the other examples because it makes use of some PHP SPL features. if your not using PHP5, this definitely won't work for you. let me know what you think! <?php function implode_by_key($glue, $keyname, $pieces) { // create a new recursive iterator to get array items $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($pieces)); $arr = array(); foreach($it AS $element) { if ($it->key() == $keyname) { $arr[] = $it->current(); } } return implode($glue, $arr); } // here is an example $array = array( array('somekey' => 'somevalue'), array( array('key2' => 'anoter value'), array('key2' => 'another sub value'), array( array('key3' => 'asdlkfs jdafajdshf aoufiahsdlkfjsadf'), array( array('key4' => 'this is key 4 - 1'), array('key4' => 'this is key 4 - 2'), array('key4' => 'this is key 4 - 3'), array( array('key5' => 'asdfkajsdflkasjdfklajshfkljasfhasdfasdf'), array('key5' => 'asdfkajsdflkasjdfklajshfkljasfhasdfasdf'), ) ) ) ) ); echo implode_by_key('<br/>', 'key4', $array); ?> This outputs: this is key 4 - 1 this is key 4 - 2 this is key 4 - 3

« previous php.notes (#189740) next »