php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60278 BC Break + warning in array_diff with nested arrays
Submitted: 2011-11-12 09:55 UTC Modified: 2011-11-15 21:02 UTC
From: kontakt at beberlei dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.0RC1 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kontakt at beberlei dot de
New email:
PHP Version: OS:

 

 [2011-11-12 09:55 UTC] kontakt at beberlei dot de
Description:
------------
A warning occurs when you call array_diff with one nested and one flat array. This code worked on 5.3.

Test script:
---------------
<?php

$a = array(0 => "id");
$b = array(0 => array("foo" => "bar"));

$d = array_diff($a, $b);

var_dump($d);

Expected result:
----------------
array(1) {
  [0]=>
  string(2) "id"
}


Actual result:
--------------
Notice: Array to string conversion in /home/benny/code/php/tests/array_diff.php on line 6
array(1) {
  [0]=>
  string(2) "id"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-15 00:55 UTC] felipe@php.net
The difference is only the E_NOTICE message, the code behaves in the sameway.
 [2011-11-15 21:02 UTC] rasmus@php.net
This isn't a BC break. This is a NOTICE telling you that your code isn't doing 
what you think it is. As per the array_diff() docs:

"This function only checks one dimension of a n-dimensional array."

Change your example to this:

$a = [0 => ["foo"]];
$b = [0 => ["bar"]];
$d = array_diff($a, $b);

This comes back and tells you that the arrays are identical. But they obviously 
aren't. Or even worse:

$a = [0 => "Array"];
$b = [0 => ["bar"]];
$d = array_diff($a, $b);

This also comes back with no difference between $a and $b exactly because the 
function only checks one level deep and you are passing it multi-dimensional 
arrays. Notices like this are there to tell you when you are doing something 
that might not produce the results you are expecting, and this is a very obvious 
example of that.
 [2011-11-15 21:02 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 06:01:34 2025 UTC