Reputation: 394
I am using this code for comparing string values in a method, which work perfectly. But for some value it gives wrong value, for example below values:
Code:
$string1 = "65";
$string2 = "5-fold";
$result = strcasecmp($string1, $string2);
switch ($result) {
case -1: print "65 comes before 5-fold"; break;
case 0: print "65 and 5-fold are the same"; break;
case 1: print "65 comes after 5-fold"; break;
}
Output:
65 comes after 5-fold
I use this code for sorted array list, which sort them like ( 65 comes before 5-fold ). may be this output of because the " - " or something else i don't know. Do you have any idea about this.....
Below code Sort the multi-dimensional array:
foreach($index_terms as $c=>$key) {
$sort_id[] = $key['id'];
$sort_term[] = $key['term'];
$sort_freq[] = $key['freq'];
}
array_multisort($sort_term, SORT_ASC, $index_terms);
Upvotes: 0
Views: 387
Reputation: 9808
try to use Intval
strcasecmp compare the binary value of your string
$string1 = intVal("65");
$string2 = intVal("5-fold");
$result = strcasecmp($string1, $string2);
switch ($result) {
case -1: print "65 comes before 5-fold"; break;
case 0: print "65 and 5-fold are the same"; break;
case 1: print "65 comes after 5-fold"; break; }
Upvotes: 0
Reputation: 22810
Code :
<?php
$string1 = "65";
$string2 = "5-fold";
$result = strcasecmp($string1, $string2);
echo $result;
?>
Output :
1
Hint : There's nothing wrong with your output. 1
means the second operand is greater, -1
means the first operand is greater.
Upvotes: 0
Reputation: 5768
You're comparing 65
to 5-fold
. It returns -1 if 65
is less than 5-fold
and 1 if 65
is greater than 5-fold
.
65 is greater than 5-fold... I don't see the problem?
What is 5-fold supposed to be that you want 65 to come before it?
Upvotes: 1