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
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: eithed at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 11:01:34 2025 UTC