Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While/foreach structure question

Status
Not open for further replies.

ronnie98

Technical User
Aug 5, 2004
36
IT
Hi all,
i have this code

Code:
$i = 0;
while ($i  <= 3 ){
    $id = $i++;

    $items = array("a" => "item1", "b" => "item2", "c" => "item3");
    foreach ($items as $item) {
	echo "sometext_".$id."<br />";
    }
		 
}
[/code ]

it actually outputs

[code]
sometext_0
sometext_0
sometext_0
sometext_1
sometext_1
sometext_1
sometext_2
sometext_2
sometext_2
sometext_3
sometext_3
sometext_3
[/code ]


I need to have this 

[code]
sometext_0
sometext_1
sometext_2
sometext_3

and i was able to get it deleting (or commenting) the "foreach" loop.
How can i modify it to have the same result without removing the "foreach"
since it seems required to handle the $items array?
Thanks in advance,ronnie.
 
i don't know what you are doing here, but you can acheive the required result by following code :

Code:
$items = array("a" => "item1", "b" => "item2", "c" => "item3");
   $id = 0 ;
    foreach ($items as $item) {
    echo "sometext_".$id."<br />";
	$id++ ;
    }	
	echo "sometext_".$id."<br />";


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Thank you spookie,
this is what i needed.
ATB,ronnie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top