PHP foreach issue, please help!

Discussion in 'PHP' started by SearchBliss, Jan 22, 2010.

  1. #1
    I am still a newbie to PHP, and I have a foreach loop issue. The section of code is the following:
    	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;
    				}			
    		}
    Code (markup):
    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!
     
    SearchBliss, Jan 22, 2010 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    Is this what you want to do?
    
    <?php
    $max = count($word[0]);
    
    for ($i=0;$i<$max;$i++) {
    	$tags = array('<td>','</td>','<td width=\'85\'>','<td align=right>');
    	echo str_replace($tags, '',$word[0][$i]).' '.str_replace($tags, '',$num[0][$i]).'<br>'.$vbCrLf;
    }
    ?>
    
    PHP:
     
    Last edited: Jan 23, 2010
    xrvel, Jan 23, 2010 IP
  3. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the best way to merg 2 foreach
    make it function
    like that
    function kill_me($ok){
    foreach(){
    }
    kill_me();
    }
     
    astkboy2008, Jan 23, 2010 IP
  4. SearchBliss

    SearchBliss Well-Known Member

    Messages:
    1,899
    Likes Received:
    70
    Best Answers:
    2
    Trophy Points:
    195
    Digital Goods:
    1
    #4
    I was given the answer in another forum, thanks guys!

    The nested two foreach loops where the issue.
     
    SearchBliss, Jan 23, 2010 IP