Re: Re: Declaring arrays? Good practice?
| From: | Jochem Maas | Date: | Wed, 14 Dec 2005 16:24:58 +0000 |
| Subject: | Re: Re: Declaring arrays? Good practice? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-227424@lists.php.net to get a copy of this message | ||
WTF are you smoking Al?
Al wrote:
Jochem Maas wrote:1. using the word 'Taint' there makes no sense. 2. secondly your example code didn't even parse. 3. I don't need to bloody try it, but just for you I did - so now you can explain the following ouput (i.e. that after the loop has run $new_array is equal to the string 'A'): PHP 5.0.4 (cli) (built: Aug 16 2005 15:29:25) moulin@moulin~ -- Wed Dec 14 17:21:47 $> php -r ' $> $old_array = range(A,E); $new_array = array(); $> $> echo "BEFORE:\n"; $> var_dump($new_array, $old_array); $> $> foreach($old_array as $value) { $new_array = $value; } $> $> echo "AFTER:\n"; $> var_dump($new_array, $old_array); $> ' BEFORE: array(0) { } array(5) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" [3]=> string(1) "D" [4]=> string(1) "E" } AFTER: string(1) "E" array(5) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" [3]=> string(1) "D" [4]=> string(1) "E" }Al wrote:Taint so Jochem. Try it.However.... For loops: $new_array= array(); foreach($old array as $value){ $new_array= $value: } Otherwise, all you'll get it the last assignment of $new_array as a variable, not an array.which is exactly what you get if yuou run the code above - after the foreach loop $new_array will be set to the value of the last item of $old_array. regardless of whether you init the var you still have to use the square-bracket notation to add items to the array.You can also use; foreach($old array as $value){ $new_array[]= $value: }