How to compare 2 lines alphabetically

What the name says. In particular, if I have

$array1['name'] = 'zoo'; $array2['name'] = 'fox'; 

How to determine that the name in alphabetical order of $array2 must exceed $array1 ?

+50
string sorting php
Oct. 20 '09 at 15:21
source share
5 answers

Use strcmp . If the first argument to strcmp is lexicographically less than the second, then the return value will be negative. If both are equal, then it will return 0. And if the first is lexicographically larger than the second, then a positive number will be returned.

pi You probably want to use strcasecmp (string1,string2) , which is case- strcasecmp (string1,string2) ...

+72
Oct 20 '09 at 15:25
source share

You can compare both lines with strcmp :

Returns <0 if str1 is less than str2; > 0 if str1 is greater than str2 and 0 if they are equal.

+8
Oct. 20 '09 at 15:24
source share

I'm a little late (back then I was not a programmer back in 2009 :-) Nobody has mentioned this yet, but you can just use the operators that you use on the number as well.

< > <= >= == != and more

For example:

'a' > 'b' returns false

'a' < 'b' returns true

http://php.net/manual/en/language.operators.comparison.php

+5
May 19 '17 at 19:52
source share

sort

CHANGE only implemented values ​​from different arrays, maybe array_merge at first, but not sure what you want

+2
Oct 20 '09 at 15:22
source share

I often use natsort ( Natural Sort ), since usually I just want to save the array for later use.

Example:

 natsort($unsorted_array); var_dump($usorted_array); // will now be sorted. 
+1
Oct 20 '09 at 15:53
source share



All Articles