php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77169 Sorting referenced array messes last index
Submitted: 2018-11-17 22:22 UTC Modified: 2018-11-17 22:43 UTC
From: dropapapa at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
 [2018-11-17 22:22 UTC] dropapapa at gmail dot com
Description:
------------
When foreaching array of arrays referenced, the last index will remain pointer to the second last index afterwards.

Test script:
---------------
<?php
$arrays = [
  [1,3,2],
  [2,8,3],
  [10,8,9],
];
foreach ($arrays as &$array) {
  echo 'First: ' . implode(', ', $array) . PHP_EOL;
}
$arrays[1] = [3,3,3];
foreach ($arrays as $array) {
  echo 'Second: ' . implode(', ', $array) . PHP_EOL;
}



Expected result:
----------------
Last index would act same way the others do.

Actual result:
--------------
Last index is now reference to second last index.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-11-17 22:43 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-11-17 22:43 UTC] requinix@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

http://php.net/manual/en/control-structures.foreach.php
Big red warning.
 [2018-11-17 22:57 UTC] spam2 at rhsoft dot net
your code is broken because it#s missing a unset()
either try to understand what references are or don't use them

https://schlueters.de/blog/archives/180-References-Still-bad-in-PHP-7.html
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Mar 28 19:01:27 2020 UTC