Using usort in php with a class private function
Make your sort function static:
private static function merchantSort($a,$b) {
return ...// the sort
}
And use an array for the second parameter:
$array = $this->someThingThatReturnAnArray();
usort($array, array('ClassName','merchantSort'));
usort function in a class
order_new is a class method not a global function.
As the PHP-Manual suggest you can use in this case
usort($data, array($this, "order_new"));
or declare order_new static and use
usort($data, array("myClass", "order_new"));
PHP: How to usort within a class?
usort ($my_array, array($this, 'compare_function');
Using a non-static object method with usort
Wherever a function is required, you can use array(object, functionName)
to call the method on that object.
usort($agegroups, array($agObj, 'sort'));
BTW, you shouldn't assign the result of usort
back to agegroups
. The sorting functions modify the array in place; they return a boolean, not the reordered array.
usort objects by object property using casting
I believe the problem is that your usort() function is trying to call a non-static method called "callBackSort" from within a static context.
To keep your callBackSort function in the same file (class) as the "test" method, make it static and either public, protected or private depending on whether you will have use for it elsewhere, and call it by passing an array as the second argument to usort.
By the by, your callBackSort function is a little more complex than it needs to be. You don't need to cast the values to int to do comparisons.
class Test
{
public static function test( $array ) {
//this will call a static method called "callBackSort" from class "Test" - equivalent to calling Test::callBackSort()
usort( $array, array("Test", "callBackSort"));
return $array;
}
public static function callBackSort( $a, $b ) {
if ( $a->id == $b->id ) {
return 0;
}
return ( $a->id < $b->id ) ? -1 : 1;
}
}
See this answer for a similar case:
Using usort in php with a class private function
Can I pass additional parameters into an inline usort function?
Using the use
keyword:
$external_variable = TRUE;
usort($array, function($a, $b) use($external_variable) {
// do sorting stuff based on $external_variable
});
http://php.net/manual/en/functions.anonymous.php
Closures may also inherit variables from the parent scope. Any such variables must be passed to the
use
language construct. From PHP 7.1, these variables must not include superglobals,$this
, or variables with the same name as a parameter.
PHP usort() expects parameter 2 to be a valid callback, not in a class
If the code is not in a class, but you are using a namespace, usort expects the second parameter to have that namespace defined. But not in an array in similar style to using usort in a class.
This worked for me, where 'cmp' is the sorting function:
usort($arrayToSort, 'My\Full\Namespace\cmp');
PHP Sorting Array Using usort() Not Working
If you are sorting string
values, you should use strcmp
usort($dc_array_process, function($a, $b) {
return strcmp($a['robo'], $b['robo']);
});
or
usort($dc_array_process, function($a, $b) {
return -strcmp($a['robo'], $b['robo']); //negative to reverse
});
document:
int strcmp ( string $str1 , string $str2 )
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than
str2, and 0 if they are equal.
Related Topics
How to Remove Part of a String in PHP
Function to Return Only Alpha-Numeric Characters from String
PHP Regular Expression for Strong Password Validation
Post to a Facebook User's Wall with Curl PHP
Codeigniter - Using Multiple Databases
Http_Build_Query with Same Name Parameters
When Do I Have to Declare Session_Start();
Handling Big User Ids Returned by Fql in PHP
Weird Error Using PHP Simple HTML Dom Parser
Find First Character That Is Different Between Two Strings
How to Check an Ip Address Is Within a Range of Two Ips in PHP
Xmlparseentityref: No Name' Warnings While Loading Xml into a PHP File
How to Check That an Object Is Empty in PHP
How to Pass Variables as Stdin into Command Line from PHP