3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class A { public $a; public $b; public $c; public function __construct($a, $b, $c) { $this->a = $a; $this->b = $b; $this->c = $c; } } $arr = Array( new A('a1235d', 'f', 'M'), new A('a1235d', 'm', 'L'), new A('a1235d', 'm', 'S'), new A('a1234d', 'm', 'L'), new A('a1234d', 'm', 'S'), new A('a1236d', 'm', 'S'), new A('a1236d', 'm', 'M'), new A('a1234d', 'f', 'S'), new A('a1236d', 'f', 'S'), new A('a1236d', 'f', 'M'), new A('a1235d', 'f', 'S'), new A('a1234d', 'm', 'M'), new A('a1235d', 'm', 'M'), new A('a1234d', 'f', 'M'), new A('a1235d', 'f', 'L'), new A('a1236d', 'm', 'L'), new A('a1236d', 'f', 'L'), new A('a1234d', 'f', 'L') ); function sort_by_a($arr) { usort($arr, function($a, $b) { if ($a->a == $b->a) { return 0; } return strcmp($a->a, $b->a); }); return $arr; } function sort_by_b($arr) { usort($arr, function($a, $b) { if ($a->b == $b->b) { return 0; } return strcmp($a->b, $b->b); }); return $arr; } function sort_by_c($arr) { usort($arr, function($a, $b) { if ($a->c == $b->c) { return 0; } return strcmp($a->c, $b->c); }); return $arr; } function printarr($arr) { foreach ($arr as $a) echo $a->a."\t".$a->b."\t".$a->c."\n"; echo "\n"; } $sort = array(); foreach ($arr as $k => $v) { $sort['a'][$k] = $v['a']; $sort['b'][$k] = $v['b']; $sort['c'][$k] = $v['c']; } array_multisort($sort['a'], SORT_DESC, $sort['b'], SORT_DESC, $sort['c'], SORT_DESC, $arr); printarr($arr); //$arr = sort_by_c($arr); //printarr($arr); //$arr = sort_by_b($arr); //printarr($arr); //$arr = sort_by_a($arr); //printarr($arr);
based on k0U1i
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Error: Cannot use object of type A as array in /in/SClMu:73 Stack trace: #0 {main} thrown in /in/SClMu on line 73
Process exited with code 255.
Output for 5.3.19 - 5.6.28
Fatal error: Cannot use object of type A as array in /in/SClMu on line 73
Process exited with code 255.