php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60806 array_multisort converts "digit only" string keys to default numeric keys:0,1..
Submitted: 2012-01-19 15:04 UTC Modified: 2012-01-19 17:01 UTC
From: jeremie dot legrand at komori-chambon dot fr Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4SVN-2012-01-19 (snap) OS: Windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jeremie dot legrand at komori-chambon dot fr
New email:
PHP Version: OS:

 

 [2012-01-19 15:04 UTC] jeremie dot legrand at komori-chambon dot fr
Description:
------------
Hello,
  I think I found a bug in array_multisort, when trying to sort a multi-dimensional array, which has string keys containing only digits.
On the resulting array, those keys are suppressed, and replaced by '0' for the first one, '1' for the second...

I had this error on PHP version 5.3.6, and on the last PHP 5.4 Snap revision: 322462, downloaded from php.net (tested with no .ini file)

Test script:
---------------
$t = array(
    'AZE' => array('SORT' => 'order #2', 'FOO' => 'xx'),
    '123' => array('SORT' => 'order #3', 'FOO' => 'xx'),
    'RTY' => array('SORT' => 'order #5', 'FOO' => 'xx'),
    '654' => array('SORT' => 'order #1', 'FOO' => 'xx'),
    'UIO' => array('SORT' => 'order #4', 'FOO' => 'xx'),
);
$W_ORDER = array();
foreach($t as $key => $row)
    $W_ORDER[(string)$key] = $row['SORT'];

array_multisort(
    $W_ORDER, SORT_ASC, SORT_STRING,
    $t
);
print_r($t);

Expected result:
----------------
Array
(
    [654] => Array
        (
            [SORT] => order #1
            [FOO] => xx
        )

    [AZE] => Array
        (
            [SORT] => order #2
            [FOO] => xx
        )

    [123] => Array
        (
            [SORT] => order #3
            [FOO] => xx
        )

    [UIO] => Array
        (
            [SORT] => order #4
            [FOO] => xx
        )

    [RTY] => Array
        (
            [SORT] => order #5
            [FOO] => xx
        )
)

Actual result:
--------------
Array
(
    [0] => Array
        (
            [SORT] => order #1
            [FOO] => xx
        )

    [AZE] => Array
        (
            [SORT] => order #2
            [FOO] => xx
        )

    [1] => Array
        (
            [SORT] => order #3
            [FOO] => xx
        )

    [UIO] => Array
        (
            [SORT] => order #4
            [FOO] => xx
        )

    [RTY] => Array
        (
            [SORT] => order #5
            [FOO] => xx
        )
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-19 17:01 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2012-01-19 17:01 UTC] rasmus@php.net
This is working as documented. The docs state that "numeric keys" will be re-
indexed. A string containing just a number is a numeric key. Note it doesn't say 
integer keys.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 20:01:33 2025 UTC