Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP foreach issue...help please???
Old 01-22-2010, 04:44 PM PHP foreach issue...help please???
kline11's Avatar
SearchBliss Web Tools

Posts: 1,789
Name: John
Location: USA
Trades: 0
I am still a newbie to PHP, and I have a foreach loop issue. The section of code is the following:
Code:
	foreach($word[0] as $value)
		{
		foreach($num[0] as $value2)
				{
    $tags = array('<td>','</td>','<td width=\'85\'>','<td align=right>');
				echo str_replace($tags, "",$value)." ".str_replace($tags, "",$value2)."<br>".$vbCrLf;
				}			
		}
The echoed results show each word ($value) as expected, but the number ($value2) next to each word is the same. For example:
word1 45
word2 45
word3 45

After it loops througn each word, it then echoes the first word, with the numbers changing as they should. For example:
word1 65
word1 49
word1 23

I am trying to get the result to show each word next to each number like:
word1 65
word2 49
word3 23

I tried using arrays to no success, finding I couldn't put the values of $word[0] and $num[0] into an array correctly if it's even possible. PHP Gurus, I would greatly appreciate any suggestions to this problem. Thanks!
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
kline11 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-22-2010, 05:53 PM Re: PHP foreach issue...help please???
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
The problem is you are using two loops (nested) to iterate two arrays. You need to either use two distinct loops or a single loop like so:
PHP Code:
$tags = array('<td>','</td>','<td width=\'85\'>','<td align=right>');
for(
$i 0$i count($word[0]); $i++)
{
     
$value $word[0][$i];
     
$value2 '';
     if(
$i count($num[0]))
     {
          
$value2 $num[0][$i];
     }
     echo 
str_replace($tags"",$value)." ".str_replace($tags"",$value2)."<br>".$vbCrLf;

Also notice how I took $tags outside of the loop. If it were in the loop the value would be reassigned for every iteration.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 01-22-2010 at 05:55 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-22-2010, 06:15 PM Re: PHP foreach issue...help please???
kline11's Avatar
SearchBliss Web Tools

Posts: 1,789
Name: John
Location: USA
Trades: 0
That works perfectly! NullPointer, you're a GOD! Thanks.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
kline11 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP foreach issue...help please???
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11896 seconds with 12 queries