php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67266 array_merge exceeds maximum execution time
Submitted: 2014-05-13 15:45 UTC Modified: 2017-12-26 12:06 UTC
From: eithed at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.4.28 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
43 + 44 = ?
Subscribe to this entry?

 
 [2014-05-13 15:45 UTC] eithed at gmail dot com
Description:
------------
I've been working on updating the referenced array while in loop, when I encountered an issue with array_merge - basically the code below will run indefinitely. If you'll change the `$value == 'a'` to `$value == 'b'` the code will successfully finish (the issue will occur for any non-last element of array); 

While array_merge doesn't solve my initial issue (as it seems to be working on array copy), I don't think such behavior is valid.

Test script:
---------------
<?php
 
   $array = array('a','b');
   $t = &$array;
 
   foreach($t as $value)
        if ($value == 'a')
            $t = array_merge($t, array('c'));
        else
            echo($value);
 
   var_dump($array);
 
?>

Expected result:
----------------
b

array
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)

Actual result:
--------------
Fatal error: Maximum execution time of 30 seconds exceeded

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-08 22:17 UTC] tpunt@php.net
This appears to be fixed in PHP 7: https://3v4l.org/T37UF
 [2017-12-26 12:06 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-12-26 12:06 UTC] nikic@php.net
Closing per previous comment. In PHP 7 foreach by value will operate on a copy of the array, so this is no longer an infinite loop.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 17:01:30 2024 UTC