473,320 Members | 2,071 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,320 software developers and data experts.

foreach - Array

hi

i want to modify an array in foreach loop

foreach($array as $ele){
$ele = $some_random_string;
}

i should see here the modified $array .

thnaks in advance

May 30 '06 #1
6 4727
iavian wrote:
hi

i want to modify an array in foreach loop

foreach($array as $ele){
$ele = $some_random_string;
}

i should see here the modified $array .

thnaks in advance

foreach( $array as &$ele ) {
$ele = $some_random_string;
}

should work as you want it to.

-david-

May 30 '06 #2
foreach ($array as $key=>$val)
{
$arrayOut[$key] = $some_random_string;
}

$array = $arrayOut;

that should work (not tested).

May 30 '06 #3
Carved in mystic runes upon the very living rock, the last words of David
Haynes of comp.lang.php make plain:
iavian wrote:
hi

i want to modify an array in foreach loop

foreach($array as $ele){
$ele = $some_random_string;
}

i should see here the modified $array .

thnaks in advance

foreach( $array as &$ele ) {
$ele = $some_random_string;
}

should work as you want it to.


Only in PHP5.

http://us3.php.net/manual/en/control...es.foreach.php

"Unless the array is referenced, foreach operates on a copy of the
specified array and not the array itself....As of PHP 5, you can easily
modify array's elements by preceding $value with &. This will assign
reference instead of copying the value."

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 30 '06 #4
iavian wrote:
i want to modify an array in foreach loop

foreach($array as $ele){
$ele = $some_random_string;
}

i should see here the modified $array .


Alternatively, you could use the array_walk() or
array_walk_recurssive() functions, which will give you some more
flexibility.
May 31 '06 #5
Adam Plocher wrote:
foreach ($array as $key=>$val)
{
$arrayOut[$key] = $some_random_string;
}

$array = $arrayOut;

that should work (not tested).


but:

foreach ($array as $key => $val) {
$array[$key] = $some_random_string;
}

is cleaner.

C.
May 31 '06 #6
Colin McKinnon wrote:
<snip>
foreach ($array as $key => $val) {
$array[$key] = $some_random_string;
}

is cleaner.


When you have to modify the array element, use of foreach is
discouraged because of overhead
<http://www.phparch.com/cert/ZPCPB_sample.pdf>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jun 2 '06 #7

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

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
8
by: cody | last post by:
currently, foreach takes a IEnumerable as parameter, so we can do: foreach (int i in array){} what about an additional form of foreach that takes an IEnumerator as parameter, so we can do: ...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
8
by: Gopal Krish | last post by:
Any sample code that uses foreach statement to walkthrough a two dimensional array? My requirement is to process data in only one dimension (say column 0 in each row of the array) Example...
3
by: Steve Barnett | last post by:
I have a collection which I need to navigate through in several different ways. Is there any way to structure my class such that I can have several different iterators? Ideally, I'd like to do...
5
by: farrell.niall | last post by:
Hi, I'm trying to use nested foreach loops to loop through two arrays. Each element in the first array corresponds to another array. The brandcode part of the first array is the same as the name...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.