|
Posted by TonyV on 10/16/07 15:41
On Oct 16, 10:55 am, Chris Jones <chris...@gmail.com> wrote:
> $arr_id = array();
> $arr_name = array();
> foreach ($arr as $k=>$v){
> array_push($arr_id,$v["id"]);
> array_push($arr_name,$v["name"]);
> }
I'm hoping to avoid manually-iterated loops like that.
Is there no php equivalent to perl's map function, that can generate
something inline based on the contents of an array? Or if the only
way to map a php array is via a function, is there any way to pass a
parameter other than the array itself to that function? Something
like:
// -------------------------------------------------------------------
// This is an error, the callback function can't take an extra
// parameter like this, but it illustrates a best-case compromise for
// what I'm trying to do.
function map_field($arr_element, $field_name) {
return $arr_element[$field_name];
}
$arr_id = array_map("map_id", $arr, "id");
$arr_name = array_map("map_name", $arr, "name");
// -------------------------------------------------------------------
Navigation:
[Reply to this message]
|