php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30737 array_merge no longer type checks as it did in php4
Submitted: 2004-11-09 21:01 UTC Modified: 2004-11-10 00:32 UTC
From: cfoster at frozenheads dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.0.2 OS: OS X 10.3.6
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cfoster at frozenheads dot com
New email:
PHP Version: OS:

 

 [2004-11-09 21:01 UTC] cfoster at frozenheads dot com
Description:
------------
In PHP4 one could call array_merge with non-array or unset variables and it would still correctly merge any arrays that were passed (i.e., type-checking was done within the function). I tested this in 4.3.8 for BSD.

In PHP5 if any of the parameters are not arrays, the function returns null.  I tested with 5.0.2 Mac OS X entropy release 1. 
   http://www.entropy.ch/software/macosx/php/

It was very handy to be able to pass a bunch of variables and get back the merged result of all the arrays without having to check each one to make sure it was set.

As is, instead of one line:

   $ar_merged = array_merge( $ar1, $ar2, $ar3, $ar4 );

one would have to have a long if-block:

$ar_merged = array();
if( is_array( $ar1 ) )
    $ar_merged = array_merge( $ar_merged, $ar1 );
if( is_array( $ar_2 ) )
    ...


Reproduce code:
---------------
<?php
$ar1 = array( "one", "two" );
$ar2 = "three";   // Optionally: comment out this line.

$ar_merged = array_merge( $ar1, $ar2 );
	
if( isset( $ar_merged ) )
    print_r( $ar_merged );
else
    print "Blarmy! Unset array!";
?>

Expected result:
----------------
In 4.3.8 it returned:

Array
(
    [0] => one
    [1] => two
    [2] => three
)

but I think it should still at least use the variables that are arrays:

Array
(
    [0] => one
    [1] => two
)

Also, if $ar2 is not defined at all it should return:

Array
(
    [0] => one
    [1] => two
)


Actual result:
--------------
Blarmy! Unset array!

It's ignoring ALL passed variables if ANY of them are not arrays, and returning a null result.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-09 23:50 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See also the PHP 5 migration guide: http://de.php.net/manual/en/migration5.incompatible.php
 [2004-11-10 00:32 UTC] cfoster at frozenheads dot com
Whoops. I had checked the manual, but missed the migration guide.

Still, it seems like the goal of a high level language should be to try to coerce data instead of creating warnings/errors where possible.  But I'm sure you guys had your reasons.

Sorry for the bogus bug.
-Colin.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC