473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Oct 16 '07 #1
3 2580
On Oct 16, 9:41 am, TonyV <kingskip...@gmail.comwrote:
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... ;)

Oct 16 '07 #2
On 16 Oct, 14:46, TonyV <kingskip...@gmail.comwrote:
On Oct 16, 9:41 am, TonyV <kingskip...@gmail.comwrote:
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"]);
}

Oct 16 '07 #3
On Oct 16, 10:55 am, Chris Jones <chris...@gmail.comwrote:
$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");
// -------------------------------------------------------------------

Oct 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.