Added a function to extract the column or columns of an array easily

This commit is contained in:
Alejandro Gallardo Escobar 2015-07-06 11:48:15 +02:00
parent 4f789f268d
commit 35654709d1
1 changed files with 25 additions and 0 deletions

View File

@ -2436,4 +2436,29 @@ function array2XML($data, $root = null, $xml = NULL) {
return html_entity_decode($xml->asXML());
}
/**
* Returns an array by extracting a column or columns.
*
* @param array Array
* @param mixed (string/array) Column name/s
*
* @return array Array formed by the extracted columns of every array iteration.
*/
function extract_column ($array, $column) {
$column_is_arr = is_array($column);
return array_map(function($item) {
if ($column_is_arr) {
return array_reduce($column, function($carry, $col) use ($item) {
$carry[$col] = $item[$col];
return $item[$col];
}, array());
}
else {
return $item[$column];
}
}, $array);
}
?>