Last active
March 19, 2016 03:39
-
-
Save NickBeeuwsaert/ec2940b3f0b3cba12668 to your computer and use it in GitHub Desktop.
Some functional array utilities in PHP. (TBH I just wanted to see how many times I could not type `for`)
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 | |
// Data like one might recieve from a form | |
$data = [ | |
'id' => [ | |
10, | |
20, | |
100, | |
], | |
'first_name' => [ | |
"Nick", | |
"Jane", | |
"John", | |
], | |
'last_name' => [ | |
'Beeuwsaert', | |
'Doe', | |
'Doe', | |
], | |
'age' => [ | |
20, | |
30, | |
40, | |
] | |
]; | |
$people = array_map( | |
partial('array_combine', ['id', 'first_name', 'last_name', 'age']), | |
zip($data['id'], $data['first_name'], $data['last_name'], $data['age']) | |
); | |
// array_column | |
var_dump(array_map(itemgetter("first_name"), $people)); | |
// array_column with re-indexing | |
var_dump( | |
array_combine( | |
array_map(itemgetter('id'), $people), | |
array_map(itemgetter('first_name'), $people) | |
) | |
); | |
// Improved Array_column-- fetch multiple values | |
var_dump( | |
array_combine( | |
array_map(itemgetter('id'), $people), | |
array_map(itemgetter('first_name', 'last_name'), $people) | |
) | |
); | |
// reindexing array and subarrays | |
var_dump( | |
array_combine( | |
array_map(itemgetter('id'), $people), | |
array_map( | |
partial("array_combine", ["first_name", "last_name", "age"]), | |
array_map(itemgetter("first_name", "last_name", "age"), $people) | |
) | |
) | |
); |
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 | |
/** | |
* Creates a partial function | |
* | |
* | |
* @param callable $fn Function to fix | |
* @param mixed ... arguments to fix to beginning of function | |
* @return callable | |
*/ | |
function partial() { | |
$partial_args = func_get_args(); | |
$fn = array_shift($partial_args); | |
return function() use ($fn, $partial_args) { | |
$full_args = array_merge($partial_args, func_get_args()); | |
return call_user_func_array($fn, $full_args); | |
}; | |
} | |
function getitem($obj, $item) { | |
return $obj[$item]; | |
} | |
/** | |
* Return a callable object that fetches $item (or $items) from its operand | |
* | |
* See pythons operator.itemgetter for more info | |
* | |
* @param $item item to get | |
* @param ... additional items to get | |
* @return callable | |
*/ | |
function itemgetter($item) { | |
$items = func_get_args(); | |
return count($items) === 1? | |
function($obj) use ($item) { | |
return getitem($obj, $item); | |
} : function($obj) use ($items) { | |
return array_map(partial("getitem", $obj), $items); | |
}; | |
} | |
/** | |
* Zips arrays together | |
* | |
* This is similar to calling array_map with a callback of null | |
* except it stops at the shortest argument | |
* | |
* @param mixed ... arrays to zip together | |
* @return array zipped arrays | |
*/ | |
function zip() { | |
// Behold, the wonky order of array_map and array_filter | |
$args = array_filter( | |
array_map( | |
"array_values", | |
func_get_args() | |
), | |
"is_array" | |
); | |
$min = min(array_map("count", $args)); | |
$results = []; | |
for($i = 0; $i < $min; $i++) { | |
$results[] = array_map(function($arg) use($i) { | |
return $arg[$i]; | |
}, $args); | |
} | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment