array_diff problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigM
    New Member
    • Aug 2007
    • 10

    array_diff problem

    Hi All,

    I've been playing around with this for ages and am just about stuck!

    I have set up small tests off array_diff, manually feeding entries into arrays and it works fine, but the below does not.

    Basically it creates 2 arrays fine, but when using array_diff, it just returns everything in whichever array I specify first ie: it doesn't match anything from the second array. Presumably then there is something about the way I am reading the data in, and array_diff can't match them as strings??

    This is the code:

    [PHP]
    <?php
    include("connec t.php");

    $date_1 = strtotime("-2 sunday");
    $date_2 = strtotime("-1 sunday");

    $projects_array 1 = array(); //array to store ipg project numbers from this query (all items from last week marked as not completed)
    $link1 = "SELECT ipg_project_num FROM ids_report WHERE designer='".ucw ords($_SESSION['user'])."' AND completed='No' AND UNIX_TIMESTAMP( sys_date) BETWEEN ". $date_1 ." AND ". $date_2 ."";
    $res1 = mysql_query($li nk1) or die(mysql_error ());
    while ($row1 = mysql_fetch_row ($res1)) {
    $a = "$row1[0]";
    $projects_array 1[] = $a;
    }

    $link2 = "SELECT ipg_project_num FROM projects WHERE designer1='".uc words($_SESSION['user'])."' AND open_status = 'open' OR designer2='".uc words($_SESSION['user'])."' AND open_status = 'open' ORDER BY name ASC";
    $res2 = mysql_query($li nk2);
    $projects_array 2 = array(); //array to store ipg project numbers from this query (all projects the deisnger is on)
    while ($row2 = mysql_fetch_row ($res2)) {
    $b = "$row2[0]";
    $projects_array 2[] = $b;
    }

    $ipg_num_list = array_diff($pro jects_array2, $projects_array 1);

    echo'1: '; print_r($projec ts_array1); echo'<br><br>';
    echo'2: '; print_r($projec ts_array2); echo'<br><br> ';
    echo'Array_Diff : '; print_r($ipg_nu m_list); echo'<br><br> ';
    ?>

    [/PHP]

    And this is what it would output:

    [HTML]
    1: Array ( [0] => 0 [1] => 1501 [2] => 1583 )

    2: Array ( [0] => 1572 [1] => 1538 [2] => 1500 [3] => 1582 [4] => 1583 [5] => 1148 [6] => 1354 [7] => 1501 [8] => 1608 [9] => 1579 [10] => 1627 [11] => 1567 [12] => 1167 [13] => 1545 )

    Array_Diff: Array ( [0] => 1572 [1] => 1538 [2] => 1500 [3] => 1582 [4] => 1583 [5] => 1148 [6] => 1354 [7] => 1501 [8] => 1608 [9] => 1579 [10] => 1627 [11] => 1567 [12] => 1167 [13] => 1545 )

    [/HTML]

    Any help you can provide is most appreciated!!

    Thanks,
    JM
  • BigM
    New Member
    • Aug 2007
    • 10

    #2
    Oh also, I know this bit looks kinda odd, I was just trying it out to see if it wrote the values into the array any differently than doing it in one step :)

    [PHP]
    $a = "$row1[0]";
    $projects_array 1[] = $a;
    [/PHP]

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      When comparing string data read from a database always trim() the data.
      I often forget this simple step and generally upset myself.
      Code:
      Either use MySql TRIM() or php trim().

      Comment

      • BigM
        New Member
        • Aug 2007
        • 10

        #4
        Originally posted by code green
        When comparing string data read from a database always trim() the data.
        I often forget this simple step and generally upset myself.
        Code:
        Either use MySql TRIM() or php trim().
        Thank you very much good sir!!

        Working perfectly, although oddly it started working once I put a trim on the numbers going into the first array. I put it on the second to make sure anyway, but thought that was interesting.

        Thanks again.

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          Thank you very much good sir!! Working perfectly
          Pleasure ,
          although oddly it started working once I put a trim on the numbers going into the first array
          If numbers are stored as INT etc there is generally no problem.
          But problems arise when reading numbers from a web form if the user has slipped in spaces, tabs returns etc
          or as in your case the number is being extracted from a string.

          Comment

          Working...