Jump to content

Warning: usort() expects parameter 2 to be a valid callback


Recommended Posts

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?

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.