Re: Re: Declaring arrays? Good practice?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



WTF are you smoking Al?

Al wrote:
Jochem Maas wrote:

Al wrote:

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:
}




Taint so Jochem.  Try it.

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"
}




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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux