Usort() With 2 Complex Condition

Viewed 46

I am storing member behavior on database as like:

$behavior_of_michael = "c_1_12,p_1_4,c_2_21,p_9_1,c_7_8";

c_1_12 = means, Michael opened 'category 1' 12 times. He also opened 'product 1' 4 times, 'category 2' 21 times.. etc.

Now; I am trying to sort the categories (the ones start with 'c') by their visiting number. (most visited categories)

I tried Usort() function; but couldnt get what i need.

usort($string , function($a, $b) {
$aLastChar = substr($a, -1);
$bLastChar = substr($b, -1);

return ($aLastChar < $bLastChar) ? -1 : 1;
});

Then i will use it on my mysql query, as like:

ORDER BY FIELD(category, $a, $b,$c ....)";

Could someone help me?

0 Answers
Related