earachefl Posted January 24, 2011 Share Posted January 24, 2011 Probably missing something really stupid here. My usort function: private function compareLastName($x, $y) { if($x['Name'] == $y['Name']) { return 0; } elseif ($x['Name'] < $y['Name']) { return -1; } else { return 1; } } is being called in this function, same file: public function loadAll() { //various SQL stuff $arrayNoNulls = $stmt->fetchAll(); //various SQL stuff $arrayNulls = $stmt->fetchAll(); $arrayAll = array_merge($arrayNoNulls, $arrayNulls); usort($arrayAll, "compareLastName"); print_r($arrayAll); } The page outputs the unsorted array, preceded by the warning. What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/225542-warning-usort-expects-parameter-2-to-be-a-valid-callback/ Share on other sites More sharing options...
AbraCadaver Posted January 24, 2011 Share Posted January 24, 2011 I assume these are in the same class? If so: usort(array($this, 'compareLastName')); For a static call: usort(array('className', 'compareLastName')); Quote Link to comment https://forums.phpfreaks.com/topic/225542-warning-usort-expects-parameter-2-to-be-a-valid-callback/#findComment-1164589 Share on other sites More sharing options...
earachefl Posted January 24, 2011 Author Share Posted January 24, 2011 I assume these are in the same class? If so: usort(array($this, 'compareLastName')); Yes, these are in the same class. Trying this I get "Fatal error: Only variables can be passed by reference". Not sure I understand. Does the function "compareLastName()" need to be in a different place? Quote Link to comment https://forums.phpfreaks.com/topic/225542-warning-usort-expects-parameter-2-to-be-a-valid-callback/#findComment-1164635 Share on other sites More sharing options...
ManiacDan Posted January 24, 2011 Share Posted January 24, 2011 Per the usort manual: usort($arrayAll, array($this,"compareLastName")); Quote Link to comment https://forums.phpfreaks.com/topic/225542-warning-usort-expects-parameter-2-to-be-a-valid-callback/#findComment-1164642 Share on other sites More sharing options...
AbraCadaver Posted January 24, 2011 Share Posted January 24, 2011 Yeah, sorry was illustrative. Should have made it functional Quote Link to comment https://forums.phpfreaks.com/topic/225542-warning-usort-expects-parameter-2-to-be-a-valid-callback/#findComment-1164653 Share on other sites More sharing options...
earachefl Posted January 24, 2011 Author Share Posted January 24, 2011 Per the usort manual: usort($arrayAll, array($this,"compareLastName")); Ah, I see. Sorry - I'm new to PHP, not used to using the resources! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/225542-warning-usort-expects-parameter-2-to-be-a-valid-callback/#findComment-1164760 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.