This is a discussion on How to map arrays? within the PHP Language forums, part of the PHP Programming Forums category; Hi all, I'm using php version 5.2.3. The exact phpinfo page is here: http://paragonmaps.com/phpinfo....
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi all, I'm using php version 5.2.3. The exact phpinfo page is here:
http://paragonmaps.com/phpinfo.php I have an array of associative arrays that I would like to break apart into separate arrays. For example, the top-level array might look something like this: // ------------------------------------------------------------------- $arr = array( array("id" => 1, "name" = "foo"), array("id" => 2, "name" = "bar"), array("id" => 3, "name" = "monkey"), array("id" => 4, "name" = "butter") ); // ------------------------------------------------------------------- What I need is two separate arrays that look like this: // ------------------------------------------------------------------- $arr_id = array(1, 2, 3, 4); $arr_name = array("foo", "bar", "monkey", "butter"); // ------------------------------------------------------------------- In perl, this would be a relatively simple task: # -------------------------------------------------------------------- $arr_id = map { $_->{id} } @arr; $arr_name = map { $_->{name} } @arr; # -------------------------------------------------------------------- I figured out that I can do something like this in php, and it works: // ------------------------------------------------------------------- function map_id($arr_element) { return $arr_element['id']; } function map_name($arr_element) { return $arr_element['name']; } $arr_id = array_map("map_id", $arr); $arr_name = array_map("map_name", $arr); // ------------------------------------------------------------------- However, I've just got to believe that there is a simpler built-in way to accomplish this task without manually iterating over all of the elements of the array via a for or while loop, and without having to create separate functions for each and every field I want to extract from the array. Any suggestions for what would be the easiest way to do this would be appreciated! -KS |
|
|||
|
On Oct 16, 9:41 am, TonyV <kingskip...@gmail.com> wrote:
> In perl, this would be a relatively simple task: > > # -------------------------------------------------------------------- > $arr_id = map { $_->{id} } @arr; > $arr_name = map { $_->{name} } @arr; > # -------------------------------------------------------------------- I meant @arr_id and @arr_name, of course... ;) |
|
|||
|
On 16 Oct, 14:46, TonyV <kingskip...@gmail.com> wrote:
> On Oct 16, 9:41 am, TonyV <kingskip...@gmail.com> wrote: > > > In perl, this would be a relatively simple task: > > > # -------------------------------------------------------------------- > > $arr_id = map { $_->{id} } @arr; > > $arr_name = map { $_->{name} } @arr; > > # -------------------------------------------------------------------- > > I meant @arr_id and @arr_name, of course... ;) $arr_id = array(); $arr_name = array(); foreach ($arr as $k=>$v){ array_push($arr_id,$v["id"]); array_push($arr_name,$v["name"]); } |
|
|||
|
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"); // ------------------------------------------------------------------- |