arrays - PHP - sort nth-level subarray -


let's suppose i've nested php array $aaa entry $aaa[$bbb][$ccc] like

array(0 => array('x' => 3, 'y' => 2), 1 => array('x' => 2, 'y' => 1), 2 => array('x' => 4, 'y' => 1)) 

and let's want order array x value in order array

array(0 => array('x' => 2, 'y' => 1), 1 => array('x' => 3, 'y' => 2), 2 => array('x' => 4, 'y' => 1)) 

and not modify other subarrays. how can it? i'm not able usort , custom function.

yet can done usort

$arr = array(     0 => array('x' => 3, 'y' => 2),     1 => array('x' => 2, 'y' => 1),     2 => array('x' => 4, 'y' => 1) );  function cmp($a, $b) {     if ($a['x'] == $b['x']) {         return 0;     }     return ($a['x'] < $b['x']) ? -1 : 1; }  usort($arr, "cmp");  var_dump($arr); 

result

array(3) {   [0]=>   array(2) {     ["x"]=>     int(2)     ["y"]=>     int(1)   }   [1]=>   array(2) {     ["x"]=>     int(3)     ["y"]=>     int(2)   }   [2]=>   array(2) {     ["x"]=>     int(4)     ["y"]=>     int(1)   } } 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -