473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

array_diff problem

10
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("connect.php");

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

$projects_array1 = 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='".ucwords($_SESSION['user'])."' AND completed='No' AND UNIX_TIMESTAMP(sys_date) BETWEEN ". $date_1 ." AND ". $date_2 ."";
$res1 = mysql_query($link1) or die(mysql_error());
while ($row1 = mysql_fetch_row($res1)) {
$a = "$row1[0]";
$projects_array1[] = $a;
}

$link2 = "SELECT ipg_project_num FROM projects WHERE designer1='".ucwords($_SESSION['user'])."' AND open_status = 'open' OR designer2='".ucwords($_SESSION['user'])."' AND open_status = 'open' ORDER BY name ASC";
$res2 = mysql_query($link2);
$projects_array2 = 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_array2[] = $b;
}

$ipg_num_list = array_diff($projects_array2, $projects_array1);

echo'1: '; print_r($projects_array1); echo'<br><br>';
echo'2: '; print_r($projects_array2); echo'<br><br> ';
echo'Array_Diff: '; print_r($ipg_num_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
Mar 3 '08 #1
4 1923
BigM
10
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_array1[] = $a;
[/PHP]
Mar 3 '08 #2
code green
1,726 Expert 1GB
When comparing string data read from a database always trim() the data.
I often forget this simple step and generally upset myself.
Expand|Select|Wrap|Line Numbers
  1. Either use MySql TRIM() or php trim().
Mar 3 '08 #3
BigM
10
When comparing string data read from a database always trim() the data.
I often forget this simple step and generally upset myself.
Expand|Select|Wrap|Line Numbers
  1. 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.
Mar 5 '08 #4
code green
1,726 Expert 1GB
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.
Mar 5 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
10
pezholio
by: pezholio | last post by:
Hi, I'm trying to put together a page that deletes records from a database based on if an item is unticked, I've got the existing items in an array (let's call it array1 for the sake of argument):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.