Replace
for($i=0;$i<count($ar);$i++) {

with

$c = max( count($ar),count($br);
for($i=0;$i<$c;$i++) {

HalfaBee

    <?php
    $ar_1 = array(1,3,5,7,9):
    $ar_2 = array(2,3,4,8);
    $ar_3 = array_diff($ar_1,$ar_2);
    
    for($i=0;$i<count($ar_3);$i++) 
    {
        echo "\$ar_3['" . $i . "'] = " . $ar_3[$i] . "<br />\n";
    } //end for
    ?>

    http://www.php.net/array_diff

      ohh ohh my turn!

      <?
      $ar = array(1, 2, 3, 4);
      $br = array(3, 2, 1, 4, 5);
      
      echo "<table border=1><tr><td>ar</td><td>br</td></tr>\n";
      for($i=0;$i<max(count($ar), count($br));$i++)
      {
      	if ($ar[$i] === $br[$i])
      	{
      		echo '<tr><td>' . $ar[$i] . '</td><td>' . $br[$i] . "</td></tr>\n";
      	}
      	else
      	{
      		if (!empty($ar[$i]))
      			echo '<tr><td>' . $ar[$i] . "</td><td>&amp;nbsp;</td></tr>\n";
      		if (!empty($br[$i]))
      			echo '<tr><td>&amp;nbsp;</td><td>' . $br[$i] . "</td></tr>\n";
      	}
      }
      echo "</table>\n";
      ?>
      

      it works... tested it 🙂
      basically just combined the ideas in the thread and didnt use array_diff like you didnt want 😉

      [edit - fixed it to stop displaying double tr's once it passed the smaller arrays bounds --]

        Originally posted by tekky
        ohh ohh my turn!

        me too! me too! 🙂

         
        $ar = array(1, 2, 6, 4); 
        $br = array(3, 7, 1, 4, 5,9,12); 
        
        echo "<table border=1><tr><td>ar</td>";
        echo "<td>br</td></tr>\n"; 
        
        sort($ar); sort($br); $i=0; $j=0; 
        while( ($i<count($ar)) && ($j<count($br)) )
        {
          if($ar[$i]==$br[$j])
          { 
            echo '<tr><td>' . $ar[$i] . '</td>';
            echo '<td>' . $br[$j] . '</td></tr>'; 
            $i++; $j++;
          } else if($ar[$i]<$br[$j]) {		
            echo '<tr><td>' . $ar[$i] . '</td>';
            echo '<td>&nbsp;</td></tr>'; 
            $i++;
          } else if($ar[$i]>$br[$j]) {
            echo '<tr><td>&nbsp;</td>';
            echo '<td>' . $br[$j] . '</td></tr>'; 
            $j++;
          }
        }
        while($i<count($ar))
        {
          echo '<tr><td>' . $ar[$i] . '</td>';
          echo '<td>&nbsp;</td></tr>'; 
          $i++;
        }
        while($j<count($br)) 
        {
          echo '<tr><td>&nbsp;</td>';
          echo '<td>' . $br[$j] . '</td></tr>'; 
          $j++;
        }
        echo "</table>";
        ?>
        

          I dont think sorting was something he wanted... or for the values to flip flop since he labled the top of each column ar/br :p

            Originally posted by tekky
            I dont think sorting was something he wanted... or for the values to flip flop since he labled the top of each column ar/br :p

            It was strongest of me! It's the "Contest" soul, I think... 🙂
            This is a really good "Code Contest" thread!

            Without sorting you need to do a n*n compares...
            (for example...

            for($i=0; $i<count($ar); $i++)
              if(in_array($ar[$i],$br))
              {
                echo '<tr><td>' . $ar[$i] . '</td>'; 
                echo '<td>' . $ar[$i] . '</td></tr>';  
            } else { echo '<tr><td>' . $ar[$i] . '</td>'; echo '<td>&nbsp;</td></tr>';
            } for($i=0; $i<count($br); $i++) if(!in_array($br[$i],$ar)) { echo '<tr><td>&nbsp;</td>'; echo '<td>' . $br[$i] . '</td></tr>';
            }

            ...)

            Instead, with a custom sorting you can do it in n*log(n) compares, I think. I think I will try next... 😃

            bye bye

            (what about flip flop 😕 ?)

              woops in reading the ar < br part I thought you were outputting the lowest value one first like 1 4 even if ar was 4 and br was 1... woops 🙂

              and by the sound of his question he wanted to compare like array keys to like array keys... maybe I mis understand him :p

                How about this.

                <?
                $ar = array(1, 2, 6, 4);
                $br = array(3, 7, 1, 4, 5,9,12);
                
                echo "<table border=1><tr><td>ar</td>";
                echo "<td>br</td></tr>\n";
                
                sort( $ar );
                reset( $ar );
                sort( $br );
                reset( $br );
                $av = current( $ar );
                $bv = current( $br );
                
                while( $av AND $bv ) {
                  if($av==$bv) {
                      echo '<tr><td>' . $av . '</td>';
                      echo '<td>' . $bv . '</td></tr>';
                      $av = next( $ar );
                      $bv = next( $br );
                  } else if($av<$bv) {
                      echo '<tr><td>' . $av . '</td>';
                      echo '<td>&nbsp;</td></tr>';
                      $av = next( $ar );
                  } else if($av>$bv) {
                      echo '<tr><td>&nbsp;</td>';
                      echo '<td>' . $bv . '</td></tr>';
                      $bv = next( $br );
                  }
                }
                echo "</table>";
                ?>

                HalfaBee

                  Originally posted by HalfaBee
                  How about this.

                  Great. This version show even the number when count() are diff.

                  <?
                  $ar = array(1, 2, 6, 4);
                  $br = array(3, 7, 1, 4, 5,9,12);
                  
                  echo "<table border=1><tr><td>ar</td>";
                  echo "<td>br</td></tr>\n";
                  
                  sort($ar); reset($ar); sort($br); reset($br);
                  $av = current( $ar ); $bv = current( $br );
                  
                  while( $av OR $bv ) {
                    if($av==$bv) {
                      $ap = $av; $bp = $bv;
                      $av = next( $ar ); $bv = next( $br );
                    } else if(($av)&&(($av<$bv) || !($bv))) {
                      $ap = $av; $bp = "&nbsp;";
                      $av = next( $ar );
                    } else if(($bv)&&(($av>$bv)|| !($av))) {
                      $ap = "&nbsp;"; $bp = $bv;
                      $bv = next( $br );
                    }
                    echo '<tr><td>' . $ap . '</td>';
                    echo '<td>' . $bp . '</td></tr>';
                  }
                  echo "</table>";
                  ?>
                  

                    wow!!!!!

                    This thread is getting good.

                    You guys have accomplished what I was trying to do, and you did it in several different ways.

                    I did not consider sorting when I first posted, but that does have an added value for me.

                    I appreciate you guys work, and will be digging through everything you have done.

                    thank you

                      Write a Reply...