php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75653 array_values don't work on empty array
Submitted: 2017-12-08 13:44 UTC Modified: 2017-12-08 13:55 UTC
From: math dot piot at gmail dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 7.2.0 OS: Debian 9
Private report: No CVE-ID: None
 [2017-12-08 13:44 UTC] math dot piot at gmail dot com
Description:
------------
If I do a array_value on an empty array, it doesn't work. It works well if there is as least 1 value in the array.

Test script:
---------------
$array[] = 'data1'; // I want [0 => 'data1']
unset($array[0]); // I want []
$array = array_values($array); // I want [] but keys resetted
$array[] = 'data2'; // I want [0 => 'data2']
$array[] = 'data3'; // I want [0 => 'data2', 1 => 'data3']
var_dump($array);


Expected result:
----------------
array(2) {
  [0]=>
  string(5) "data2"
  [1]=>
  string(5) "data3"
}

Actual result:
--------------
array(2) {
  [1]=>
  string(5) "data2"
  [2]=>
  string(5) "data3"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-08 13:55 UTC] requinix@php.net
-Status: Open +Status: Duplicate -Package: *General Issues +Package: Arrays related
 [2017-12-08 13:55 UTC] requinix@php.net
Basically the same as bug #75433
 [2017-12-08 13:55 UTC] spam2 at rhsoft dot net
looks like a terrible BC break in 7.2 and i can remember recently that we wrote code to get the first remaining value after cleanup by $first = array_values($array)[0] which would no longer work with PHP 7.2

preserve keys here makes no sense

7.1.12:
array(2) {
  [0]=>
  string(5) "data2"
  [1]=>
  string(5) "data3"
}

7.2.1-dev:
array(2) {
  [1]=>
  string(5) "data2"
  [2]=>
  string(5) "data3"
}
 [2018-01-12 17:34 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8c73fc8027a7293e81c0b2e8dc2674fe34f1eb59
Log: Fixed bug #75653
 [2018-01-12 17:34 UTC] nikic@php.net
-Status: Duplicate +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 14 04:01:27 2024 UTC