Page 1 of 1

Strange .. Result..

Posted: Sat Aug 09, 2003 9:59 am
by andrewt
Hi guys, I'm just messing around with this code..

Code: Select all

<?
 	
	$fruits = array("Apple" => "Red", "Mango" => "Green", "Lemon" => "Yellow", "Durian" => "Green");
	 
	$fruits&#1111;"Dragon Fruit"] = "Pink";
	

	
	reset($fruits);
	while (list ($key, $value) = each($fruits)) &#123;
		echo "Color of $key is $value <br>";
		echo "Color of " . key($fruits) . " is " . current($fruits) . "<br>"; 
	&#125;
And the result is :
  • Color of Apple is Red
    Color of Mango is Green
    Color of Mango is Green
    Color of Lemon is Yellow
    Color of Lemon is Yellow
    Color of Durian is Green
    Color of Durian is Green
    Color of Dragon Fruit is Pink
    Color of Dragon Fruit is Pink
    Color of is
Don't you think this is strange?
The rest of it outside the code
are just html tags.

Thanks,
Andrew

8O

Posted: Sat Aug 09, 2003 10:55 am
by pootergeist
and the rest of the code pls.

Posted: Sat Aug 09, 2003 2:55 pm
by qartis
Pootergeist, he says that's the only code, aside from some html


Andrewt, that code has a few deprecated elements to it. You may or may not know this, but your problem code can be rewritten to be a lot simpler and cleaner; observe:

Code: Select all

<?
$fruits = array("Apple" => "Red", "Mango" => "Green", "Lemon" => "Yellow", "Durian" => "Green");
   
$fruits['Dragon Fruit'] = "Pink";

reset($fruits);

foreach ($fruits as $key => $value){
    echo "Color of $key is $value <br>";
}
?>
Your original code, however, was working properly.

Code..

Posted: Sat Aug 09, 2003 4:16 pm
by andrewt
Hi Qartis,

I agree.

However, I am only writting the code as to see the result of the code.

Just trying to find an explanation to the code, as I understanding it
it should be two pairs of the same data appear one after the other
on a new line.
  • Color of Apple is Red
    Color of Apple is Red
    Color of Mango is Green
    Color of Mango is Green
    Color of Lemon is Yellow
    Color of Lemon is Yellow
    Color of Durian is Green
    Color of Durian is Green
    Color of Dragon Fruit is Pink
    Color of Dragon Fruit is Pink
Regards,
Andrew

Posted: Sat Aug 09, 2003 4:33 pm
by JAM
qartis wrote: Your original code, however, was working properly.
Well, not entirely true...
THE Manual wrote:list() only works on numerical arrays and assumes the numerical indices start at 0.
...and that is not the case here. Read the notes on in the manual for more info. I recall that there is some discussion the on the matter.

Posted: Sat Aug 09, 2003 4:50 pm
by andrewt
I think that in this case list is being utilize by the next part of the code
each $fruit as a two elements array, therefore it worked.


However, I think the fails on the second part of the echo statement.

Posted: Sat Aug 09, 2003 4:57 pm
by JAM
well...

Code: Select all

<?php
      echo "ECHO 1 : Color of " . key($fruits) . " is " . current($fruits) . "<br>"; 
      echo "ECHO 2 : Color of $key is $value <br>";
?>
add those ECHO's to the string and see what happens.
Never noticed this, so I must say it was interesting...