|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2016-01-11 11:03 UTC] alex dot schneider at sevenval dot com
 Description:
------------
Cannot access array keys while uksort().
Sorry for my English...
Test script:
---------------
<?php
class myClass
{
	private $a = [
		'foo-test' => [1],
		'-' => [2],
		'bar-test' => [3]
	];
	private function _mySort($x, $y)
	{
		if (!isset($this->a[$x])) {
			throw new Exception('Missing X: "' . $x . '"');
		}
		if (!isset($this->a[$y])) {
			throw new Exception('Missing Y: "' . $y . '"');
		}
		return $x < $y;
	}
	public function __construct()
	{
		uksort($this->a, [$this, '_mySort']);
	}
}
new myClass();
Expected result:
----------------
No Exceptions
Actual result:
--------------
Exception:
PHP Fatal error:  Uncaught Exception: Missing Y: "bar-test" in ...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 04:00:01 2025 UTC | 
I have the same problem, I am able to access the values, but the sorting is not correct. <?php function cmp($a, $b) { $x = $GLOBALS['x']; $v1 = $x[$a]; $v2 = $x[$b]; if ($v1 < $v2) { return -1; } elseif ($v1 > $v2) { return 1; } else { // Would then sort by key, irrelevant here. } } $x = array(1 => 1, 2 => 3, 3 => 2); uksort($x, 'cmp'); var_dump($x); // PHP5 array(3) { [1]=> int(1) [3]=> int(2) [2]=> int(3) } // PHP7 array(3) { [3]=> int(2) [2]=> int(3) [1]=> int(1) } Let me know if you want me to open another bug instead.