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
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: 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

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: Sat Jun 01 02:01:32 2024 UTC