Skip to content

Instantly share code, notes, and snippets.

@alanszlosek
Created September 1, 2015 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanszlosek/5c1a6e053762ed6bfa9e to your computer and use it in GitHub Desktop.
Save alanszlosek/5c1a6e053762ed6bfa9e to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL);
$a = new stdClass();
$a->name = 'Zzzz';
$b = new stdClass();
$b->name = 'Bbbb';
$meets = array();
$meets[] = $a;
$meets[] = $b;
$names = array();
$names[] = $a->name;
$names[] = $b->name;
// We'll get PHP Notices about attempting to convert each meet object to a number
array_multisort($meets, SORT_NUMERIC, SORT_DESC, $names, SORT_STRING, SORT_ASC);
var_dump($meets);
echo "NEXT\n\n";
// Sort the other direction ... No notices when used correctly
array_multisort($names, SORT_STRING, SORT_DESC, $meets);
var_dump($meets);
@alanszlosek
Copy link
Author

Output:

PHP Notice:  Object of class stdClass could not be converted to double in /Users/alanszlosek/Testing/20150901.multisort.php on line 20

Notice: Object of class stdClass could not be converted to double in /Users/alanszlosek/Testing/20150901.multisort.php on line 20
PHP Notice:  Object of class stdClass could not be converted to double in /Users/alanszlosek/Testing/20150901.multisort.php on line 20

Notice: Object of class stdClass could not be converted to double in /Users/alanszlosek/Testing/20150901.multisort.php on line 20
array(2) {
  [0]=>
  object(stdClass)#2 (1) {
    ["name"]=>
    string(4) "Bbbb"
  }
  [1]=>
  object(stdClass)#1 (1) {
    ["name"]=>
    string(4) "Zzzz"
  }
}
NEXT

array(2) {
  [0]=>
  object(stdClass)#1 (1) {
    ["name"]=>
    string(4) "Zzzz"
  }
  [1]=>
  object(stdClass)#2 (1) {
    ["name"]=>
    string(4) "Bbbb"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment