php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74313 Problem from array_diff function
Submitted: 2017-03-26 00:12 UTC Modified: 2017-03-27 07:41 UTC
From: dominik dot kolinski at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.6.30 OS: CentOS 7
Private report: No CVE-ID: None
 [2017-03-26 00:12 UTC] dominik dot kolinski at gmail dot com
Description:
------------
Hello, i have a problem from array_diff function. I have a two dynamic arrays, which are a power from mysql. Which they have four different values: 4415, 4416,4417, and 4418. Unfortunately, using this feature (array_diff) has a bad output. (Return bad output)
Function returns the full contents of the first array Instead of a difference.


Please inform me what may be wrong.

Regards, Dominik KoliƄski.

This exemple topic in my .pl developer's forum:

http://www.elektroda.pl/rtvforum/topic3323198.html?sid=c6056d33afbbf8b9e97f904d623082f4

Array_one : 

Array
(
[0] => 4413
[1] => 4414
[2] => 13020
[3] => 13022
[4] => 4801
[5] => 4802
[6] => 4809
[7] => 11204
[8] => 14201
[9] => 14202
[10] => 14203
[11] => 12303
[12] => 12304
[13] => 10113
[14] => 10302
[15] => 10307
[16] => 4
[17] => 10404
[18] => 10502
[19] => 10701
[20] => 10704
[21] => 10801
[22] => 11006
[23] => 11201
[24] => 11305
[25] => 11310
[26] => 11605
[27] => 11702
[28] => 11705
[29] => 11707
[30] => 11803
}
And Array second:

Array
(
[0] => 4413
[1] => 4414
[2] => 13020
[3] => 13022
[4] => 4801
[5] => 4802
[6] => 4809
[7] => 11204
[8] => 14201
[9] => 14202
[10] => 14203
[11] => 12303
[12] => 12304
[13] => 10113
[14] => 10302
[15] => 10307
[16] => 4
[17] => 10404
[18] => 10502
[19] => 10701
[20] => 10704
[21] => 10801
[22] => 11006
[23] => 11201
[24] => 11305
[25] => 11310
[26] => 11605
[27] => 11702
[28] => 11705
[29] => 11707
[30] => 11803
[31] => 4415
[32] => 4416
[33] => 4417 
[34] => 4418
}

Test script:
---------------
This example from my code: 

	$query_select_id_1 = mysql_query("SELECT id FROM tabela1 where id <> 0") or die("Zapytanie niepoprawne"); 
	while($select_id_1 = mysql_fetch_assoc($query_select_id_1)){
		$array_1[] = $select_id_1['id'];
	}

	$query_select_id_2 = mysql_query("SELECT id FROM tabela2 where id <> 0") or die("Zapytanie niepoprawne"); 
	while($select_id_2 = mysql_fetch_assoc($query_select_id_2)){
		$array_2[] = $select_id_2['id'];
	}

$diff = array_diff($array_1, $array_2);
print_r($diff);


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-26 05:11 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-03-26 05:11 UTC] requinix@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

http://php.net/manual/en/function.array-diff.php
> ...returns the values in array1 that are not present in any of the other arrays

https://3v4l.org/OmoMT
With your sample data the function is working correctly. If $diff == $array_1 then that means $array_2 does not contain any duplicate values of $array_1, or is empty.
 [2017-03-27 07:41 UTC] dominik dot kolinski at gmail dot com
Hello, Tkanka for your help help for my. This problem was due to bad types (string) for input values in my arrays. I am used function intval and problem was resolved.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 02:01:29 2024 UTC