[PHP] unexpected array_diff output

2004-04-30 Thread Frederic Noyer
Hello there !
	I am trying to fill and then compare two arrays: one filled by a 
foreach construct, and the second by a while construct.
I check both with a print_r to be sure that both are correctly filled 
(which seems to be the case). But then, when I try to compare them with 
a array_diff , the result is incoherent (at least to me...). I have 
tried several variant, but I don't understand what's wrong. Sure you 
guys can tell me where is my mistake.

Here is the code:

?

// creates first array (from a _POST[''] )
	foreach ($_POST['input_auteur'] AS $aut_ligne) {
			$part_ligne = explode(,,$aut_ligne);
			$arr1[] = (string) $part_ligne['2'];
	}
	echo Array from POST :;
	print_r ($arr1);
	
// creates second array (from SQL query)
	$sqlListAut = 'SELECT aut_id FROM mr_ecrit WHERE doc_id = ' . 
$_GET['docu'];
	$resultListAut = mysql_query($sqlListAut);
	while ($recListAut = mysql_fetch_array($resultListAut)) {
			 $arr2[] = (string) $recListAut['aut_id'];
	}
	echo Array from SQL:BR;
	print_r ($arr2);	

// comparing both arrays to detect if there is elements in the first 
array which are not present in the second array	$result = 
array_diff($arr1, $arr2);
	echo array_diff results :;
	print_r($result);
?

Here is a typical output:
__
Array from POST: Array (  [0] = 2  [1] = 3  [2] = 4 )
Array from SQL : Array (  [0] = 3  [1] = 4 )
**
array_diff results : Array (  [0] = 2  [1] = 3  [2] = 4 )
__
As you can see, the array_diff seems to always return all elements of 
the first array.

Thanks for your help.
Best regards,
Fred Noyer

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: unexpected array_diff output

2004-04-30 Thread Frederic Noyer
On 30 avr. 04, at 11:00, Torsten Roehr wrote:

Frederic Noyer [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Hello there !
I am trying to fill and then compare two arrays: one filled by a
foreach construct, and the second by a while construct.
I check both with a print_r to be sure that both are correctly filled
(which seems to be the case). But then, when I try to compare them 
with
a array_diff , the result is incoherent (at least to me...). I have
tried several variant, but I don't understand what's wrong. Sure you
guys can tell me where is my mistake.

Here is the code:

?

// creates first array (from a _POST[''] )
foreach ($_POST['input_auteur'] AS $aut_ligne) {
$part_ligne = explode(,,$aut_ligne);
$arr1[] = (string) $part_ligne['2'];
}
echo Array from POST :;
print_r ($arr1);
// creates second array (from SQL query)
$sqlListAut = 'SELECT aut_id FROM mr_ecrit WHERE doc_id = ' .
$_GET['docu'];
$resultListAut = mysql_query($sqlListAut);
while ($recListAut = mysql_fetch_array($resultListAut)) {
$arr2[] = (string) $recListAut['aut_id'];
}
echo Array from SQL:BR;
print_r ($arr2);
// comparing both arrays to detect if there is elements in the first
array which are not present in the second array $result =
array_diff($arr1, $arr2);
echo array_diff results :;
print_r($result);
?
Here is a typical output:
__
Array from POST: Array (  [0] = 2  [1] = 3  [2] = 4 )
Array from SQL : Array (  [0] = 3  [1] = 4 )
**
array_diff results : Array (  [0] = 2  [1] = 3  [2] = 4 )
__
As you can see, the array_diff seems to always return all elements of
the first array.
I think this is correct because the 3 key/value pairs from the first 
array
are NOT in the second one. I think the combination of key and value are
relevant for array_diff().

So if you had:

Array from POST: Array (  [0] = 2  [1] = 3  [2] = 4 )
Array from SQL : Array (  [0] = 2  [1] = 3 )
**
The diff would be:
array_diff results : Array (  [2] = 4 )
Ok, but when I give the function this input

Array from POST: Array (  [0] = 2 )

Array from SQL : Array (  [0] = 2 )

The output I get is:

the diff between both : Array (  [0] = 2 )

Why is it not and empty output ?  Which is not what I have understand 
the array_diff should do (or maybe I didn't understand what the 
function should do).
Thanks again for your kind help,

Frederic Noyer

PS: version of PHP is 4.3

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: unexpected array_diff output

2004-04-30 Thread Frederic Noyer
On 30 avr. 04, at 11:00, Torsten Roehr wrote:

I think this is correct because the 3 key/value pairs from the first 
array
are NOT in the second one. I think the combination of key and value are
relevant for array_diff().
As I have understand only the array_diff_assoc function does that.
see php.net:
Description
array array_diff_assoc ( array array1, array array2 [, array ...])
array_diff_assoc() returns an array  containing all the values from 
array1 that are not present in any of the other arguments. Note that 
the keys are used in the comparison unlike  array_diff().

Fred
So if you had:

Array from POST: Array (  [0] = 2  [1] = 3  [2] = 4 )
Array from SQL : Array (  [0] = 2  [1] = 3 )
**
The diff would be:
array_diff results : Array (  [2] = 4 )
Regards, Torsten
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: unexpected array_diff output

2004-04-30 Thread Frederic Noyer
On 30 avr. 04, at 11:31, Torsten Roehr wrote:

Frederic Noyer [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
On 30 avr. 04, at 11:00, Torsten Roehr wrote:

I think this is correct because the 3 key/value pairs from the first
array
are NOT in the second one. I think the combination of key and value 
are
relevant for array_diff().
As I have understand only the array_diff_assoc function does that.
see php.net:
Description
array array_diff_assoc ( array array1, array array2 [, array ...])
array_diff_assoc() returns an array  containing all the values from
array1 that are not present in any of the other arguments. Note that
the keys are used in the comparison unlike  array_diff().
So array_diff() seems to be for arrays where the key is not defined so 
that
only values are recognized.

Regards, Torsten
Any ideas what this implies in termes of code ?
You mean, I should not fill my array with values this way ?
$arr2[] = (string) $recListAut['aut_id'];
Because this is precisely the way to fill it without specifying the 
key. see www.php.net: 
http://ch2.php.net/manual/en/language.types.array.php

Regards, Fred

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] unexpected array_diff output

2004-04-30 Thread Frederic Noyer
On 30 avr. 04, at 12:11, Ford, Mike [LSS] wrote:

On 30 April 2004 09:47, Frederic Noyer wrote:

Hello there !
I am trying to fill and then compare two arrays: one
filled by a
foreach construct, and the second by a while construct.
I check both with a print_r to be sure that both are correctly filled
(which seems to be the case). But then, when I try to compare
them with
a array_diff , the result is incoherent (at least to me...). I have
tried several variant, but I don't understand what's wrong. Sure you
guys can tell me where is my mistake.
Here is the code:

?

// creates first array (from a _POST[''] )
foreach ($_POST['input_auteur'] AS $aut_ligne) {
$part_ligne = explode(,,$aut_ligne);
$arr1[] = (string) $part_ligne['2'];
}
echo Array from POST :;
print_r ($arr1);
I don't have any great insight into what appear to be your odd 
array_diff()
results, but try using var_dump() instead of print_r() -- it will give 
you
more information about *exactly* what values are stored in the arrays, 
and
it may just be that you'll spot an odd discrepancy.

Cheers!

Mike
Thanks, Mike ! That was the correct answer. In one of the array there 
was always a space in front of the digit (ex:  2). The comparison 
was then corrupted because of this. I never used var_dump() before, but 
it is far more precise. Thanks for the hint!

Thanks to you too, Thorsten for your kind help.

Best regards,

Fred

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php