Skip to content

Instantly share code, notes, and snippets.

@Jmayhak
Created July 13, 2011 22:24
Show Gist options
  • Save Jmayhak/1081474 to your computer and use it in GitHub Desktop.
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
<?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