By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

need to assign/edit values in multidimensional associative arrays

P: n/a
I am finding that altering and affecting values in elements in
multidimensional arrays is a huge pain in the ass. I cannot seem to find a
consistent way to assign values to arrays. Foreach would clearly be the most
efficient way to do it, but it only works on a copy of the original array
and not the original (which is counter intuitive in my estimation). Using
each doesn't work consistently either. Not only that, it's unduly complex
for multidimensional arrays that are more than 3 dimensions. For example,
here's an array $data:

0 => Array (8)
id => 8
name => test entry3 3 2fsdfsdfs sdf sdf
address => empty
city => empty
state => 66
closingdate => empty
loanamount => 0
date => empty
1 => Array (8)
id => 9
name => test entry 2
address => empty
city => empty
state => 66
closingdate => empty
loanamount => 0
date => empty
2 => Array (8)
id => 10
name => empty
address => empty
city => empty
state => 66
closingdate => empty
loanamount => 0
date => empty

I would like to iterate through this array and add an element to each called
url which will be assigned based on a function of id. In pseudocode:

foreach ($data as $array) {
$array['url'] = SomeCoolFunction("$array['id']");
}

How can I do this? Assigning values to elements and working with existing
arrays in Perl is extremely easy. This appears to be a weakness in the
design of PHP. Thoughts?

TIA

GN
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Golf Nut wrote:
(snip)
I would like to iterate through this array and add an element to each called
url which will be assigned based on a function of id. In pseudocode:

foreach ($data as $array) {
$array['url'] = SomeCoolFunction("$array['id']");
}

How can I do this? Assigning values to elements and working with existing
arrays in Perl is extremely easy. This appears to be a weakness in the
design of PHP. Thoughts?


Try

foreach ($data as $k=>$array) {
$data[$k]['url'] = SomeCoolFunction($array['id']);
}

Or, if you don't like it (I don't), use the array_walk() function
http://www.php.net/array_walk
function AnotherCoolFunction(&$element, $key) {
$element['url'] = SomeCoolFunction($element['id']);
}
array_walk($data, 'AnotherCoolFunction');
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

P: n/a
On Thu, 27 May 2004 19:55:49 GMT, "Golf Nut"
<tr*************************@yahoo.com> wrote:
I am finding that altering and affecting values in elements in
multidimensional arrays is a huge pain in the ass. I cannot seem to find a
consistent way to assign values to arrays. Foreach would clearly be the most
efficient way to do it, but it only works on a copy of the original array
and not the original (which is counter intuitive in my estimation).
Fair point there.
For example, here's an array $data:

0 => Array (8)
id => 8 [snip]
I would like to iterate through this array and add an element to each called
url which will be assigned based on a function of id. In pseudocode:

foreach ($data as $array) {
$array['url'] = SomeCoolFunction("$array['id']");
}

How can I do this? Assigning values to elements and working with existing
arrays in Perl is extremely easy. This appears to be a weakness in the
design of PHP. Thoughts?


Depends how you look at it - possibly it's more that Perl has some outstanding
syntax in this area; processing data structures can be done very concisely with
things like map and hash slices that don't have equivalents in many other
languages.

Since you can't get a reference to the original array element out of a plain
PHP foreach loop, one idiom is to get the key and reference the array again in
the loop.

foreach ($data as $key => $array) {
$data[$key]['url'] = SomeCoolFunction($array['id']);
}

If you still want something Perl-ish there's always array_map or array_walk
but anonymous sub syntax in PHP isn't as neat as Perl either. Something like:

array_walk($data,
create_function('&$a', '$a["url"] = SomeCoolFunction($a["id"]);'));

Yuk :-(

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #3

P: n/a
Thanks so much both of you who responded - I could have sworn that I tried
this particular idiom, but apparently not!

A thought for php is perhaps to modify it so that if you're performing an
assignment on an array within a foreach construct (or any construct, for
that matter), that it performs it on the original. I don't know if this
would add any overhead to the code; the compiler would only do this in case
of assignments, so normal foreach use and other iterations wouldn't impact
performance at all. Thoughts?

Thanks again!

GN

"Pedro Graca" <he****@hotpop.com> wrote in message
news:2h************@uni-berlin.de...
Golf Nut wrote:
(snip)
I would like to iterate through this array and add an element to each called url which will be assigned based on a function of id. In pseudocode:

foreach ($data as $array) {
$array['url'] = SomeCoolFunction("$array['id']");
}

How can I do this? Assigning values to elements and working with existing arrays in Perl is extremely easy. This appears to be a weakness in the
design of PHP. Thoughts?


Try

foreach ($data as $k=>$array) {
$data[$k]['url'] = SomeCoolFunction($array['id']);
}

Or, if you don't like it (I don't), use the array_walk() function
http://www.php.net/array_walk
function AnotherCoolFunction(&$element, $key) {
$element['url'] = SomeCoolFunction($element['id']);
}
array_walk($data, 'AnotherCoolFunction');
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :

Jul 17 '05 #4

P: n/a
On Thu, 27 May 2004 21:09:04 GMT, "Golf Nut"
<tr*************************@yahoo.com> wrote:
A thought for php is perhaps to modify it so that if you're performing an
assignment on an array within a foreach construct (or any construct, for
that matter), that it performs it on the original. I don't know if this
would add any overhead to the code; the compiler would only do this in case
of assignments, so normal foreach use and other iterations wouldn't impact
performance at all. Thoughts?


Or perhaps as:

foreach ($array as $value) // work on a copy

foreach ($array as &$value) // reference to the actual value

Avoids breaking backwards compatibility that way.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

P: n/a
On 2004-05-27, Andy Hassall <an**@andyh.co.uk> wrote:
foreach ($array as &$value) // reference to the actual value


I would be in favour of this one. But adding the key to the foreach construct
and refer to the original array by the key isn't that hard either, it's just
a bit uncommon.
AFAIK in PHP5 all variables are references, so I hope this will be automagically
implemented there and the comments at php.net for `foreach' seem to prove this.

Bye,
phil
--
Please send replys (not followups) to the address set in Reply-To.
Philipp Kern - PK2186-RIPE - http://www.philkern.de
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.