Created
July 13, 2011 22:24
-
-
Save Jmayhak/1081474 to your computer and use it in GitHub Desktop.
A php function to implode an array full of arrays by a key in each of the arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implodes an array full of arrays based on the passed key that is in each element of the array | |
* by the passed delimiter | |
* | |
* implode_key(',', array(array('title'=>'jonathan'), array('title'=>'justin')), 'title') | |
* // will return "jonathan,justin" | |
* | |
* @param string $delimiter | |
* @param array $arrays | |
* @param string $key | |
* @return string | |
*/ | |
function implode_by_key($delimiter, $arrays, $key) | |
{ | |
$new_array = array(); | |
foreach ($arrays as $array) { | |
if (isset($array[$key]) == true) { | |
$new_array[] = $array[$key]; | |
} | |
} | |
return implode($delimiter, $new_array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment