Re: [PHP] foreach in php4

2005-07-06 Thread Dotan Cohen
On 7/5/05, yanghshiqi <[EMAIL PROTECTED]> wrote:
> Try this:
> 
> function mul(&$value){
> $value = $value * 2;
> }
> 
> $arr = array("a" => "1", "b" => "2", "c" => "3", "d" => "4");
> array_walk($arr, 'mul');
> var_dump($arr);
> 
> 
> Best regards,
> Shiqi Yang
> 
> -----Original Message-
> From: Dotan Cohen [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, July 05, 2005 1:40 PM
> To: PHP Lists
> Subject: [PHP] foreach in php4
> 
> I am on php 4.x. I see that in php5 I can do this (not the & before $value):
> $arr = array(1, 2, 3, 4);
> foreach ($arr as &$value) {
>$value = $value * 2;
> }
> // $arr is now array(2, 4, 6, 8)
> 
> In order to create the same effect, I have been doing this:
> $pre_arr = array(1, 2, 3, 4);
> $arr = array();
> foreach ($pre_arr as &$value) {
>$arr[] = $value * 2;
> }
> // $arr is now array(2, 4, 6, 8)
> 
> Is there a better way? Thanks.
> 
> Dotan Cohen
> http://lyricslist.com/lyrics/artist_albums/327/martin_ricky.php
> Martin, Ricky Song Lyrics
> 

Thank you Shiki! This is what I was needing... I didn't know about
array_walk. One new function every day!

Dotan Cohen
http://lyricslist.com/lyrics/artist_albums/408/presidents_of_the_united_states_of_america.php
The Presidents of the United States of America Song Lyrics

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] foreach in php4

2005-07-05 Thread Richard Lynch
On Mon, July 4, 2005 10:40 pm, Dotan Cohen said:
> $pre_arr = array(1, 2, 3, 4);
> $arr = array();

foreach ($pre_arr as $key=>$value) {

$pre_arr[$key] = $value * 2;

//>$arr[] = $value * 2;
> }
> // $arr is now array(2, 4, 6, 8)
>
> Is there a better way? Thanks.

Whether that's better or not depends on what you mean by "better"... :-)

PS I dunno if I got the foreach syntax right or not.  Never use it.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] foreach in php4

2005-07-05 Thread yanghshiqi
Try this:

function mul(&$value){
$value = $value * 2;
}

$arr = array("a" => "1", "b" => "2", "c" => "3", "d" => "4");
array_walk($arr, 'mul');
var_dump($arr);

 
 
 
Best regards,
Shiqi Yang

-Original Message-
From: Dotan Cohen [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 05, 2005 1:40 PM
To: PHP Lists
Subject: [PHP] foreach in php4

I am on php 4.x. I see that in php5 I can do this (not the & before $value):
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
   $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)

In order to create the same effect, I have been doing this:
$pre_arr = array(1, 2, 3, 4);
$arr = array();
foreach ($pre_arr as &$value) {
   $arr[] = $value * 2;
}
// $arr is now array(2, 4, 6, 8)

Is there a better way? Thanks.

Dotan Cohen
http://lyricslist.com/lyrics/artist_albums/327/martin_ricky.php
Martin, Ricky Song Lyrics

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] foreach in php4

2005-07-04 Thread Dotan Cohen
I am on php 4.x. I see that in php5 I can do this (not the & before $value):
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
   $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)

In order to create the same effect, I have been doing this:
$pre_arr = array(1, 2, 3, 4);
$arr = array();
foreach ($pre_arr as &$value) {
   $arr[] = $value * 2;
}
// $arr is now array(2, 4, 6, 8)

Is there a better way? Thanks.

Dotan Cohen
http://lyricslist.com/lyrics/artist_albums/327/martin_ricky.php
Martin, Ricky Song Lyrics

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php