Hi there guys πŸ˜ƒ ,

$array1=mysql_fetch_array(mysql_query("select field from table1"));

$array2=mysql_fetch_array(mysql_query("select field from table2"));

$array3=array_diff($array1,$array2);

while (list ($field) = $array3) {
echo"-> $field<br>";
}

This will end in an endless loop, i know the problem itΒ΄s in list can someone help me and tell me how to do it correctly?

Tks for your attention.

    Hi,

    try to use

    while (list ($key,$field) = each($array3)) {

    since $array3 contains results of mysql_fetch_array you might need to use

    $field['tablefieldname'] inside the while loop πŸ™‚

      $array1=mysql_fetch_array(mysql_query("select field from table1"));

      $array2=mysql_fetch_array(mysql_query("select field from table2"));

      $array3=array_diff($array1,$array2);

      while (list ($key,$field) = each($array3)) {
      echo"-> $field<br>";
      }

      IΒ΄ve got it like this, it shows twice the same regist instead of show them all, still doesnt know what the problem is.

      Tks for the help tsinka πŸ™‚

        Hi,

        does it work if you use

        while (list ($key,$record) = each($array3)) {
        echo"-> ".$record['field']."<br>";
        }

        $record will contain an associative array containing the fields of a certain record after fetching records with mysql_fetch_array.

        what's the count of $array3 (count($array3)) after you do the array_diff ? Does it look like you expected ?

        You might need to use array_diff_assoc instead of array_diff since mysql_fetch_array fetches associative arrays.

        array_diff_assoc will create an array that contains all data that is not common to both source arrays.

          //get all the results of the query and set them in an array
          $array1=mysql_fetch_array(mysql_query("select field from table1"));

          //get all the results of the query and set them in an array
          $array2=mysql_fetch_array(mysql_query("select field from table2"));

          //set an array with values that arent in both queries
          $array3=array_diff($array1,$array2);

          //list values on array3
          while (list ($key,$record) = each($array3)) {
          echo"-> ".$record['field']."<br>";
          }

          I really dont know what im doing wrong, or if the functions i used are the correct to establish my goal, tks a lot for your attention tsinka can you or anyone else still help me? πŸ˜•

            Did you try array_diff_assoc instead of array_diff ?

              yup... the result was the same .. (weird?!) really dont know what else to do!

                Hi,

                I simulated that one and it indeed doesn't work with an array of arrays.

                When I built two arrays with just the values it worked.

                So get the results with mysql_fetch_array and built two additional arrays which just contain the names.

                the array of mysql_fetch_array looks like that:

                array(array("field"=>"a"),array("field"=>"b"),array("field"=>"c"),array("field"=>"d"),array("field"=>"e"));

                You have to build an array that looks like:

                $array1=array("a","b","c","d","e");

                something like:

                
                $fetched1 = mysql_fetch_array($result);
                $array1 = "";
                
                for ($i=0;$i<count($fetched1);$i++) {
                    $array1[] = $fetched1[$i]["field"];
                }
                
                
                $fetched2 = mysql_fetch_array($result);
                $array2 = "";
                
                for ($i=0;$i<count($fetched2);$i++) {
                    $array2[] = $fetched2[$i]["field"];
                }
                
                $array3 = array_diff($array1,$array2);
                
                
                

                $array3 will then contain all fields values of $array1 that do not exist in $array2

                If you also want to get all the values of $array2 that do not exist in $array1 you would have to create another array

                $array4 = array_diff($array2,$array1);

                  mysql_fetch_array() contains two entries for every field; one that is numerically indexed, and one that is indexed by field name. I.e.,
                  array('field1'=>'value1', 'field2'=>'value2', ..., 0=>'value1', 1=>'value2', ...).

                  To get one or the other use mysql_fetch_row() (for numeric indices) or mysql_fetch_assoc() (for associative indices); alternatively, use the result_type flag.

                  That should make diffing easier.

                  Yet another alternative, do the whole thing in MySQL to start with:

                  SELECT table1.field FROM table1
                  LEFT JOIN table2 ON table1.id=table2.field
                  WHERE table2.field IS NULL

                  (Or, since more recent versions of MySQL support subselects which are easier to read and write, though tend to be slower to execute)

                  select field from table1 where field not in (select field from table2)
                  

                  If you still want to use a loop in PHP, foreach() is faster:

                  foreach($array as $key=>$val)
                  {...
                  }

                    Write a Reply...