You are here: Re: How to map arrays? « PHP Programming Language « IT news, forums, messages
Re: How to map arrays?

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация