How to map arrays?

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....


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-16-2007
TonyV
 
Posts: n/a
Default How to map arrays?

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

Reply With Quote
  #2 (permalink)  
Old 10-16-2007
TonyV
 
Posts: n/a
Default Re: How to map arrays?

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... ;)

Reply With Quote
  #3 (permalink)  
Old 10-16-2007
Chris Jones
 
Posts: n/a
Default Re: How to map arrays?

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"]);
}

Reply With Quote
  #4 (permalink)  
Old 10-16-2007
TonyV
 
Posts: n/a
Default Re: How to map arrays?

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");
// -------------------------------------------------------------------

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:02 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0