Strange .. Result..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andrewt
Forum Newbie
Posts: 11
Joined: Fri Aug 08, 2003 8:18 am
Location: Sydney, Australia

Strange .. Result..

Post 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
Last edited by andrewt on Sat Aug 09, 2003 11:08 am, edited 1 time in total.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

and the rest of the code pls.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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.
andrewt
Forum Newbie
Posts: 11
Joined: Fri Aug 08, 2003 8:18 am
Location: Sydney, Australia

Code..

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
andrewt
Forum Newbie
Posts: 11
Joined: Fri Aug 08, 2003 8:18 am
Location: Sydney, Australia

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
Post Reply