PHP array sorting and compatibility with Persian alphabets












9















I'm trying to sort an array first by it's values and then by it's keys but php is not doing well with Persian characters.


Persian alphabets are similar to Arabic alphabets except some additional characters like 'گ چ پ ژ ک' and PHP is doing great at sorting Arabic letters in Persian Alphabets but the rest is not in their order.



For example



$str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
$arr = explode(' ', $str);


will create an array ($arr) containing all Persian alphabets in correct alphabetical order. and if I shuffle it and use asort function like following:



shuffle($arr);
asort($arr);
var_dump($arr);


it will end as something like this:



    array
2 => string 'ا'
1 => string 'ب'
22 => string 'ت'
29 => string 'ث'
20 => string 'ج'
12 => string 'ح'
21 => string 'خ'
18 => string 'د'
6 => string 'ذ'
3 => string 'ر'
27 => string 'ز'
17 => string 'ص'
11 => string 'ض'
25 => string 'ط'
5 => string 'ظ'
16 => string 'ع'
8 => string 'غ'
26 => string 'ف'
14 => string 'ق'
9 => string 'ل'
0 => string 'م'
7 => string 'ن'
10 => string 'ه'
28 => string 'و'
24 => string 'پ'
23 => string 'چ'
13 => string 'ژ'
19 => string 'ک'
4 => string 'گ'
15 => string 'ی'


which is wrong!



24th item should be after 1st, 23rd should be after 20 and so on.



How can I write a functions doing something similar to PHP's own sorting functions? Or maybe there's a way to make PHP functions work for persian characters?










share|improve this question


















  • 1





    Use the appropriate sort() function with the sort_flag set to SORT_LOCALE_STRING having used setlocale()

    – Mark Baker
    Apr 1 '14 at 20:48








  • 1





    @MarkBaker I did setlocale(LC_ALL, 'fa_IR'); asort($arr, SORT_LOCALE_STRING);. but it's not working; am I doing it wrong?

    – Farid Rn
    Apr 1 '14 at 20:54













  • Does your server support that locale? Does the setlocale(LC_ALL, 'fa_IR'); return a Boolean false?

    – Mark Baker
    Apr 1 '14 at 20:55











  • @MarkBaker I thought fa_IR could be locale of farsi/persian but it's returning false on both Windows and Linux environment.

    – Farid Rn
    Apr 1 '14 at 20:58











  • @MarkBaker I can't find proper locale for Perian, I can see fa_IR in answers of this thread but that's not working!

    – Farid Rn
    Apr 1 '14 at 21:02


















9















I'm trying to sort an array first by it's values and then by it's keys but php is not doing well with Persian characters.


Persian alphabets are similar to Arabic alphabets except some additional characters like 'گ چ پ ژ ک' and PHP is doing great at sorting Arabic letters in Persian Alphabets but the rest is not in their order.



For example



$str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
$arr = explode(' ', $str);


will create an array ($arr) containing all Persian alphabets in correct alphabetical order. and if I shuffle it and use asort function like following:



shuffle($arr);
asort($arr);
var_dump($arr);


it will end as something like this:



    array
2 => string 'ا'
1 => string 'ب'
22 => string 'ت'
29 => string 'ث'
20 => string 'ج'
12 => string 'ح'
21 => string 'خ'
18 => string 'د'
6 => string 'ذ'
3 => string 'ر'
27 => string 'ز'
17 => string 'ص'
11 => string 'ض'
25 => string 'ط'
5 => string 'ظ'
16 => string 'ع'
8 => string 'غ'
26 => string 'ف'
14 => string 'ق'
9 => string 'ل'
0 => string 'م'
7 => string 'ن'
10 => string 'ه'
28 => string 'و'
24 => string 'پ'
23 => string 'چ'
13 => string 'ژ'
19 => string 'ک'
4 => string 'گ'
15 => string 'ی'


which is wrong!



24th item should be after 1st, 23rd should be after 20 and so on.



How can I write a functions doing something similar to PHP's own sorting functions? Or maybe there's a way to make PHP functions work for persian characters?










share|improve this question


















  • 1





    Use the appropriate sort() function with the sort_flag set to SORT_LOCALE_STRING having used setlocale()

    – Mark Baker
    Apr 1 '14 at 20:48








  • 1





    @MarkBaker I did setlocale(LC_ALL, 'fa_IR'); asort($arr, SORT_LOCALE_STRING);. but it's not working; am I doing it wrong?

    – Farid Rn
    Apr 1 '14 at 20:54













  • Does your server support that locale? Does the setlocale(LC_ALL, 'fa_IR'); return a Boolean false?

    – Mark Baker
    Apr 1 '14 at 20:55











  • @MarkBaker I thought fa_IR could be locale of farsi/persian but it's returning false on both Windows and Linux environment.

    – Farid Rn
    Apr 1 '14 at 20:58











  • @MarkBaker I can't find proper locale for Perian, I can see fa_IR in answers of this thread but that's not working!

    – Farid Rn
    Apr 1 '14 at 21:02
















9












9








9


4






I'm trying to sort an array first by it's values and then by it's keys but php is not doing well with Persian characters.


Persian alphabets are similar to Arabic alphabets except some additional characters like 'گ چ پ ژ ک' and PHP is doing great at sorting Arabic letters in Persian Alphabets but the rest is not in their order.



For example



$str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
$arr = explode(' ', $str);


will create an array ($arr) containing all Persian alphabets in correct alphabetical order. and if I shuffle it and use asort function like following:



shuffle($arr);
asort($arr);
var_dump($arr);


it will end as something like this:



    array
2 => string 'ا'
1 => string 'ب'
22 => string 'ت'
29 => string 'ث'
20 => string 'ج'
12 => string 'ح'
21 => string 'خ'
18 => string 'د'
6 => string 'ذ'
3 => string 'ر'
27 => string 'ز'
17 => string 'ص'
11 => string 'ض'
25 => string 'ط'
5 => string 'ظ'
16 => string 'ع'
8 => string 'غ'
26 => string 'ف'
14 => string 'ق'
9 => string 'ل'
0 => string 'م'
7 => string 'ن'
10 => string 'ه'
28 => string 'و'
24 => string 'پ'
23 => string 'چ'
13 => string 'ژ'
19 => string 'ک'
4 => string 'گ'
15 => string 'ی'


which is wrong!



24th item should be after 1st, 23rd should be after 20 and so on.



How can I write a functions doing something similar to PHP's own sorting functions? Or maybe there's a way to make PHP functions work for persian characters?










share|improve this question














I'm trying to sort an array first by it's values and then by it's keys but php is not doing well with Persian characters.


Persian alphabets are similar to Arabic alphabets except some additional characters like 'گ چ پ ژ ک' and PHP is doing great at sorting Arabic letters in Persian Alphabets but the rest is not in their order.



For example



$str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
$arr = explode(' ', $str);


will create an array ($arr) containing all Persian alphabets in correct alphabetical order. and if I shuffle it and use asort function like following:



shuffle($arr);
asort($arr);
var_dump($arr);


it will end as something like this:



    array
2 => string 'ا'
1 => string 'ب'
22 => string 'ت'
29 => string 'ث'
20 => string 'ج'
12 => string 'ح'
21 => string 'خ'
18 => string 'د'
6 => string 'ذ'
3 => string 'ر'
27 => string 'ز'
17 => string 'ص'
11 => string 'ض'
25 => string 'ط'
5 => string 'ظ'
16 => string 'ع'
8 => string 'غ'
26 => string 'ف'
14 => string 'ق'
9 => string 'ل'
0 => string 'م'
7 => string 'ن'
10 => string 'ه'
28 => string 'و'
24 => string 'پ'
23 => string 'چ'
13 => string 'ژ'
19 => string 'ک'
4 => string 'گ'
15 => string 'ی'


which is wrong!



24th item should be after 1st, 23rd should be after 20 and so on.



How can I write a functions doing something similar to PHP's own sorting functions? Or maybe there's a way to make PHP functions work for persian characters?







php arrays sorting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 1 '14 at 20:46









Farid RnFarid Rn

1,87832654




1,87832654








  • 1





    Use the appropriate sort() function with the sort_flag set to SORT_LOCALE_STRING having used setlocale()

    – Mark Baker
    Apr 1 '14 at 20:48








  • 1





    @MarkBaker I did setlocale(LC_ALL, 'fa_IR'); asort($arr, SORT_LOCALE_STRING);. but it's not working; am I doing it wrong?

    – Farid Rn
    Apr 1 '14 at 20:54













  • Does your server support that locale? Does the setlocale(LC_ALL, 'fa_IR'); return a Boolean false?

    – Mark Baker
    Apr 1 '14 at 20:55











  • @MarkBaker I thought fa_IR could be locale of farsi/persian but it's returning false on both Windows and Linux environment.

    – Farid Rn
    Apr 1 '14 at 20:58











  • @MarkBaker I can't find proper locale for Perian, I can see fa_IR in answers of this thread but that's not working!

    – Farid Rn
    Apr 1 '14 at 21:02
















  • 1





    Use the appropriate sort() function with the sort_flag set to SORT_LOCALE_STRING having used setlocale()

    – Mark Baker
    Apr 1 '14 at 20:48








  • 1





    @MarkBaker I did setlocale(LC_ALL, 'fa_IR'); asort($arr, SORT_LOCALE_STRING);. but it's not working; am I doing it wrong?

    – Farid Rn
    Apr 1 '14 at 20:54













  • Does your server support that locale? Does the setlocale(LC_ALL, 'fa_IR'); return a Boolean false?

    – Mark Baker
    Apr 1 '14 at 20:55











  • @MarkBaker I thought fa_IR could be locale of farsi/persian but it's returning false on both Windows and Linux environment.

    – Farid Rn
    Apr 1 '14 at 20:58











  • @MarkBaker I can't find proper locale for Perian, I can see fa_IR in answers of this thread but that's not working!

    – Farid Rn
    Apr 1 '14 at 21:02










1




1





Use the appropriate sort() function with the sort_flag set to SORT_LOCALE_STRING having used setlocale()

– Mark Baker
Apr 1 '14 at 20:48







Use the appropriate sort() function with the sort_flag set to SORT_LOCALE_STRING having used setlocale()

– Mark Baker
Apr 1 '14 at 20:48






1




1





@MarkBaker I did setlocale(LC_ALL, 'fa_IR'); asort($arr, SORT_LOCALE_STRING);. but it's not working; am I doing it wrong?

– Farid Rn
Apr 1 '14 at 20:54







@MarkBaker I did setlocale(LC_ALL, 'fa_IR'); asort($arr, SORT_LOCALE_STRING);. but it's not working; am I doing it wrong?

– Farid Rn
Apr 1 '14 at 20:54















Does your server support that locale? Does the setlocale(LC_ALL, 'fa_IR'); return a Boolean false?

– Mark Baker
Apr 1 '14 at 20:55





Does your server support that locale? Does the setlocale(LC_ALL, 'fa_IR'); return a Boolean false?

– Mark Baker
Apr 1 '14 at 20:55













@MarkBaker I thought fa_IR could be locale of farsi/persian but it's returning false on both Windows and Linux environment.

– Farid Rn
Apr 1 '14 at 20:58





@MarkBaker I thought fa_IR could be locale of farsi/persian but it's returning false on both Windows and Linux environment.

– Farid Rn
Apr 1 '14 at 20:58













@MarkBaker I can't find proper locale for Perian, I can see fa_IR in answers of this thread but that's not working!

– Farid Rn
Apr 1 '14 at 21:02







@MarkBaker I can't find proper locale for Perian, I can see fa_IR in answers of this thread but that's not working!

– Farid Rn
Apr 1 '14 at 21:02














3 Answers
3






active

oldest

votes


















3














I’ve written the following function to return the UTF-8 code point for any given character:



function utf8_ord($str) {
$str = (string) $str;
$ord = ord($str);
$ord_b = decbin($ord);

if (strlen($ord_b) <= 7)
return $ord;
$len = strlen(strstr($ord_b, "0", true));

if ($len < 2 || $len > 4 || strlen($str) < $len)
return false;
$val = substr($ord_b, $len + 1);

for ($i = 1; $i < $len; $i++) {
$ord_b = decbin(ord($str[$i]));
if ($ord_b[0].$ord_b[1] != "10")
return false;
$val. = substr($ord_b, 2);
}
$val = bindec($val);
return (($val > 0x10FFFF) ? null : $val);
}


Now let’s find out the UTF-8 code points of the characters in your array:



$str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
$arr = explode(' ', $str);
print_r(array_map("utf8_ord", $arr));


The output will be:



Array
(
[0] => 1575
[1] => 1576
[2] => 1662
[3] => 1578
[4] => 1579
[5] => 1580
[6] => 1670
[7] => 1581
[8] => 1582
[9] => 1583
[10] => 1584
[11] => 1585
[12] => 1586
[13] => 1688
[14] => 1589
[15] => 1590
[16] => 1591
[17] => 1592
[18] => 1593
[19] => 1594
[20] => 1601
[21] => 1602
[22] => 1705
[23] => 1711
[24] => 1604
[25] => 1605
[26] => 1606
[27] => 1608
[28] => 1607
[29] => 1740
)


It clearly shows that the characters are not in proper order and needs to be sorted. I don’t know Persian, so I’m unable to determine whether or not there’s a fault in the UTF-8 Persian alphabet. But all I can say is that PHP is doing its work correctly.






share|improve this answer

































    0














    I create a custom javascript sort function for Persian arrays:



    var alphabets = ["ا", "ب", "پ", "ت", "ث", "ج", "چ", "ح", "خ", "د",

    "ذ", "ر", "ز", "ژ", "س", "ش", "ص", "ض", "ط", "ظ", "ع", "غ",

    "ف", "ق", "ک", "گ", "ل", "م", "ن", "و", "ه", "ی"];

    function PersianOrder(){

    var persianArrray = ["ایمان", "محمدرضا", "ژوله", "چمدان", "پدرام", "پاشی","پاشا"];

    persianArrray.sort(function (a, b) {

    return CharCompare(a, b, 0);

    });

    }

    function CharCompare(a, b, index) {

    if (index == a.length || index == b.length)

    return 0;

    var aChar = alphabets.indexOf(a.charAt(index));

    var bChar = alphabets.indexOf(b.charAt(index));

    if (aChar != bChar)

    return aChar - bChar

    else

    return CharCompare(a,b,index+1)

    }


    Check Online



    I'll hope that this function help you






    share|improve this answer



















    • 1





      Dude, the question is almost 4 years old and I needed a PHP solution, not a JS one!

      – Farid Rn
      Oct 9 '17 at 9:56





















    0














    well to get the available locales you can use



    print_r(ResourceBundle::getLocales(''));


    I had both 'fa' and 'fa_IR' available, however 'fa_IR' was still returning false so I used 'fa' to test it:



    setlocale(LC_ALL, 'fa');
    asort($arr, SORT_LOCALE_STRING);
    var_dump($arr);


    but this was not still sorting in the proper order for me...



    so after abit of more googling, the solution that has finally worked for me to sort Unicode Persian alphabets was using the Collator class:



    $col = new Collator('fa_IR');
    $col->asort($arr);
    var_dump($arr);


    I know the question is old but this might still be helping the new people getting here looking for an answer to this question.






    share|improve this answer
























    • As you can see, I haven't marked any answer as accepted because none of the solutions worked for me. Your workaround seems promising, I'll give it a try. Thanks a lot.

      – Farid Rn
      Oct 16 '17 at 16:01











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22796522%2fphp-array-sorting-and-compatibility-with-persian-alphabets%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    I’ve written the following function to return the UTF-8 code point for any given character:



    function utf8_ord($str) {
    $str = (string) $str;
    $ord = ord($str);
    $ord_b = decbin($ord);

    if (strlen($ord_b) <= 7)
    return $ord;
    $len = strlen(strstr($ord_b, "0", true));

    if ($len < 2 || $len > 4 || strlen($str) < $len)
    return false;
    $val = substr($ord_b, $len + 1);

    for ($i = 1; $i < $len; $i++) {
    $ord_b = decbin(ord($str[$i]));
    if ($ord_b[0].$ord_b[1] != "10")
    return false;
    $val. = substr($ord_b, 2);
    }
    $val = bindec($val);
    return (($val > 0x10FFFF) ? null : $val);
    }


    Now let’s find out the UTF-8 code points of the characters in your array:



    $str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
    $arr = explode(' ', $str);
    print_r(array_map("utf8_ord", $arr));


    The output will be:



    Array
    (
    [0] => 1575
    [1] => 1576
    [2] => 1662
    [3] => 1578
    [4] => 1579
    [5] => 1580
    [6] => 1670
    [7] => 1581
    [8] => 1582
    [9] => 1583
    [10] => 1584
    [11] => 1585
    [12] => 1586
    [13] => 1688
    [14] => 1589
    [15] => 1590
    [16] => 1591
    [17] => 1592
    [18] => 1593
    [19] => 1594
    [20] => 1601
    [21] => 1602
    [22] => 1705
    [23] => 1711
    [24] => 1604
    [25] => 1605
    [26] => 1606
    [27] => 1608
    [28] => 1607
    [29] => 1740
    )


    It clearly shows that the characters are not in proper order and needs to be sorted. I don’t know Persian, so I’m unable to determine whether or not there’s a fault in the UTF-8 Persian alphabet. But all I can say is that PHP is doing its work correctly.






    share|improve this answer






























      3














      I’ve written the following function to return the UTF-8 code point for any given character:



      function utf8_ord($str) {
      $str = (string) $str;
      $ord = ord($str);
      $ord_b = decbin($ord);

      if (strlen($ord_b) <= 7)
      return $ord;
      $len = strlen(strstr($ord_b, "0", true));

      if ($len < 2 || $len > 4 || strlen($str) < $len)
      return false;
      $val = substr($ord_b, $len + 1);

      for ($i = 1; $i < $len; $i++) {
      $ord_b = decbin(ord($str[$i]));
      if ($ord_b[0].$ord_b[1] != "10")
      return false;
      $val. = substr($ord_b, 2);
      }
      $val = bindec($val);
      return (($val > 0x10FFFF) ? null : $val);
      }


      Now let’s find out the UTF-8 code points of the characters in your array:



      $str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
      $arr = explode(' ', $str);
      print_r(array_map("utf8_ord", $arr));


      The output will be:



      Array
      (
      [0] => 1575
      [1] => 1576
      [2] => 1662
      [3] => 1578
      [4] => 1579
      [5] => 1580
      [6] => 1670
      [7] => 1581
      [8] => 1582
      [9] => 1583
      [10] => 1584
      [11] => 1585
      [12] => 1586
      [13] => 1688
      [14] => 1589
      [15] => 1590
      [16] => 1591
      [17] => 1592
      [18] => 1593
      [19] => 1594
      [20] => 1601
      [21] => 1602
      [22] => 1705
      [23] => 1711
      [24] => 1604
      [25] => 1605
      [26] => 1606
      [27] => 1608
      [28] => 1607
      [29] => 1740
      )


      It clearly shows that the characters are not in proper order and needs to be sorted. I don’t know Persian, so I’m unable to determine whether or not there’s a fault in the UTF-8 Persian alphabet. But all I can say is that PHP is doing its work correctly.






      share|improve this answer




























        3












        3








        3







        I’ve written the following function to return the UTF-8 code point for any given character:



        function utf8_ord($str) {
        $str = (string) $str;
        $ord = ord($str);
        $ord_b = decbin($ord);

        if (strlen($ord_b) <= 7)
        return $ord;
        $len = strlen(strstr($ord_b, "0", true));

        if ($len < 2 || $len > 4 || strlen($str) < $len)
        return false;
        $val = substr($ord_b, $len + 1);

        for ($i = 1; $i < $len; $i++) {
        $ord_b = decbin(ord($str[$i]));
        if ($ord_b[0].$ord_b[1] != "10")
        return false;
        $val. = substr($ord_b, 2);
        }
        $val = bindec($val);
        return (($val > 0x10FFFF) ? null : $val);
        }


        Now let’s find out the UTF-8 code points of the characters in your array:



        $str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
        $arr = explode(' ', $str);
        print_r(array_map("utf8_ord", $arr));


        The output will be:



        Array
        (
        [0] => 1575
        [1] => 1576
        [2] => 1662
        [3] => 1578
        [4] => 1579
        [5] => 1580
        [6] => 1670
        [7] => 1581
        [8] => 1582
        [9] => 1583
        [10] => 1584
        [11] => 1585
        [12] => 1586
        [13] => 1688
        [14] => 1589
        [15] => 1590
        [16] => 1591
        [17] => 1592
        [18] => 1593
        [19] => 1594
        [20] => 1601
        [21] => 1602
        [22] => 1705
        [23] => 1711
        [24] => 1604
        [25] => 1605
        [26] => 1606
        [27] => 1608
        [28] => 1607
        [29] => 1740
        )


        It clearly shows that the characters are not in proper order and needs to be sorted. I don’t know Persian, so I’m unable to determine whether or not there’s a fault in the UTF-8 Persian alphabet. But all I can say is that PHP is doing its work correctly.






        share|improve this answer















        I’ve written the following function to return the UTF-8 code point for any given character:



        function utf8_ord($str) {
        $str = (string) $str;
        $ord = ord($str);
        $ord_b = decbin($ord);

        if (strlen($ord_b) <= 7)
        return $ord;
        $len = strlen(strstr($ord_b, "0", true));

        if ($len < 2 || $len > 4 || strlen($str) < $len)
        return false;
        $val = substr($ord_b, $len + 1);

        for ($i = 1; $i < $len; $i++) {
        $ord_b = decbin(ord($str[$i]));
        if ($ord_b[0].$ord_b[1] != "10")
        return false;
        $val. = substr($ord_b, 2);
        }
        $val = bindec($val);
        return (($val > 0x10FFFF) ? null : $val);
        }


        Now let’s find out the UTF-8 code points of the characters in your array:



        $str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
        $arr = explode(' ', $str);
        print_r(array_map("utf8_ord", $arr));


        The output will be:



        Array
        (
        [0] => 1575
        [1] => 1576
        [2] => 1662
        [3] => 1578
        [4] => 1579
        [5] => 1580
        [6] => 1670
        [7] => 1581
        [8] => 1582
        [9] => 1583
        [10] => 1584
        [11] => 1585
        [12] => 1586
        [13] => 1688
        [14] => 1589
        [15] => 1590
        [16] => 1591
        [17] => 1592
        [18] => 1593
        [19] => 1594
        [20] => 1601
        [21] => 1602
        [22] => 1705
        [23] => 1711
        [24] => 1604
        [25] => 1605
        [26] => 1606
        [27] => 1608
        [28] => 1607
        [29] => 1740
        )


        It clearly shows that the characters are not in proper order and needs to be sorted. I don’t know Persian, so I’m unable to determine whether or not there’s a fault in the UTF-8 Persian alphabet. But all I can say is that PHP is doing its work correctly.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 8:39









        Mohammad

        15.4k123361




        15.4k123361










        answered May 21 '14 at 3:15









        Sharanya DuttaSharanya Dutta

        3,68321124




        3,68321124

























            0














            I create a custom javascript sort function for Persian arrays:



            var alphabets = ["ا", "ب", "پ", "ت", "ث", "ج", "چ", "ح", "خ", "د",

            "ذ", "ر", "ز", "ژ", "س", "ش", "ص", "ض", "ط", "ظ", "ع", "غ",

            "ف", "ق", "ک", "گ", "ل", "م", "ن", "و", "ه", "ی"];

            function PersianOrder(){

            var persianArrray = ["ایمان", "محمدرضا", "ژوله", "چمدان", "پدرام", "پاشی","پاشا"];

            persianArrray.sort(function (a, b) {

            return CharCompare(a, b, 0);

            });

            }

            function CharCompare(a, b, index) {

            if (index == a.length || index == b.length)

            return 0;

            var aChar = alphabets.indexOf(a.charAt(index));

            var bChar = alphabets.indexOf(b.charAt(index));

            if (aChar != bChar)

            return aChar - bChar

            else

            return CharCompare(a,b,index+1)

            }


            Check Online



            I'll hope that this function help you






            share|improve this answer



















            • 1





              Dude, the question is almost 4 years old and I needed a PHP solution, not a JS one!

              – Farid Rn
              Oct 9 '17 at 9:56


















            0














            I create a custom javascript sort function for Persian arrays:



            var alphabets = ["ا", "ب", "پ", "ت", "ث", "ج", "چ", "ح", "خ", "د",

            "ذ", "ر", "ز", "ژ", "س", "ش", "ص", "ض", "ط", "ظ", "ع", "غ",

            "ف", "ق", "ک", "گ", "ل", "م", "ن", "و", "ه", "ی"];

            function PersianOrder(){

            var persianArrray = ["ایمان", "محمدرضا", "ژوله", "چمدان", "پدرام", "پاشی","پاشا"];

            persianArrray.sort(function (a, b) {

            return CharCompare(a, b, 0);

            });

            }

            function CharCompare(a, b, index) {

            if (index == a.length || index == b.length)

            return 0;

            var aChar = alphabets.indexOf(a.charAt(index));

            var bChar = alphabets.indexOf(b.charAt(index));

            if (aChar != bChar)

            return aChar - bChar

            else

            return CharCompare(a,b,index+1)

            }


            Check Online



            I'll hope that this function help you






            share|improve this answer



















            • 1





              Dude, the question is almost 4 years old and I needed a PHP solution, not a JS one!

              – Farid Rn
              Oct 9 '17 at 9:56
















            0












            0








            0







            I create a custom javascript sort function for Persian arrays:



            var alphabets = ["ا", "ب", "پ", "ت", "ث", "ج", "چ", "ح", "خ", "د",

            "ذ", "ر", "ز", "ژ", "س", "ش", "ص", "ض", "ط", "ظ", "ع", "غ",

            "ف", "ق", "ک", "گ", "ل", "م", "ن", "و", "ه", "ی"];

            function PersianOrder(){

            var persianArrray = ["ایمان", "محمدرضا", "ژوله", "چمدان", "پدرام", "پاشی","پاشا"];

            persianArrray.sort(function (a, b) {

            return CharCompare(a, b, 0);

            });

            }

            function CharCompare(a, b, index) {

            if (index == a.length || index == b.length)

            return 0;

            var aChar = alphabets.indexOf(a.charAt(index));

            var bChar = alphabets.indexOf(b.charAt(index));

            if (aChar != bChar)

            return aChar - bChar

            else

            return CharCompare(a,b,index+1)

            }


            Check Online



            I'll hope that this function help you






            share|improve this answer













            I create a custom javascript sort function for Persian arrays:



            var alphabets = ["ا", "ب", "پ", "ت", "ث", "ج", "چ", "ح", "خ", "د",

            "ذ", "ر", "ز", "ژ", "س", "ش", "ص", "ض", "ط", "ظ", "ع", "غ",

            "ف", "ق", "ک", "گ", "ل", "م", "ن", "و", "ه", "ی"];

            function PersianOrder(){

            var persianArrray = ["ایمان", "محمدرضا", "ژوله", "چمدان", "پدرام", "پاشی","پاشا"];

            persianArrray.sort(function (a, b) {

            return CharCompare(a, b, 0);

            });

            }

            function CharCompare(a, b, index) {

            if (index == a.length || index == b.length)

            return 0;

            var aChar = alphabets.indexOf(a.charAt(index));

            var bChar = alphabets.indexOf(b.charAt(index));

            if (aChar != bChar)

            return aChar - bChar

            else

            return CharCompare(a,b,index+1)

            }


            Check Online



            I'll hope that this function help you







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 8 '17 at 20:45









            Iman BahrampourIman Bahrampour

            1,87621437




            1,87621437








            • 1





              Dude, the question is almost 4 years old and I needed a PHP solution, not a JS one!

              – Farid Rn
              Oct 9 '17 at 9:56
















            • 1





              Dude, the question is almost 4 years old and I needed a PHP solution, not a JS one!

              – Farid Rn
              Oct 9 '17 at 9:56










            1




            1





            Dude, the question is almost 4 years old and I needed a PHP solution, not a JS one!

            – Farid Rn
            Oct 9 '17 at 9:56







            Dude, the question is almost 4 years old and I needed a PHP solution, not a JS one!

            – Farid Rn
            Oct 9 '17 at 9:56













            0














            well to get the available locales you can use



            print_r(ResourceBundle::getLocales(''));


            I had both 'fa' and 'fa_IR' available, however 'fa_IR' was still returning false so I used 'fa' to test it:



            setlocale(LC_ALL, 'fa');
            asort($arr, SORT_LOCALE_STRING);
            var_dump($arr);


            but this was not still sorting in the proper order for me...



            so after abit of more googling, the solution that has finally worked for me to sort Unicode Persian alphabets was using the Collator class:



            $col = new Collator('fa_IR');
            $col->asort($arr);
            var_dump($arr);


            I know the question is old but this might still be helping the new people getting here looking for an answer to this question.






            share|improve this answer
























            • As you can see, I haven't marked any answer as accepted because none of the solutions worked for me. Your workaround seems promising, I'll give it a try. Thanks a lot.

              – Farid Rn
              Oct 16 '17 at 16:01
















            0














            well to get the available locales you can use



            print_r(ResourceBundle::getLocales(''));


            I had both 'fa' and 'fa_IR' available, however 'fa_IR' was still returning false so I used 'fa' to test it:



            setlocale(LC_ALL, 'fa');
            asort($arr, SORT_LOCALE_STRING);
            var_dump($arr);


            but this was not still sorting in the proper order for me...



            so after abit of more googling, the solution that has finally worked for me to sort Unicode Persian alphabets was using the Collator class:



            $col = new Collator('fa_IR');
            $col->asort($arr);
            var_dump($arr);


            I know the question is old but this might still be helping the new people getting here looking for an answer to this question.






            share|improve this answer
























            • As you can see, I haven't marked any answer as accepted because none of the solutions worked for me. Your workaround seems promising, I'll give it a try. Thanks a lot.

              – Farid Rn
              Oct 16 '17 at 16:01














            0












            0








            0







            well to get the available locales you can use



            print_r(ResourceBundle::getLocales(''));


            I had both 'fa' and 'fa_IR' available, however 'fa_IR' was still returning false so I used 'fa' to test it:



            setlocale(LC_ALL, 'fa');
            asort($arr, SORT_LOCALE_STRING);
            var_dump($arr);


            but this was not still sorting in the proper order for me...



            so after abit of more googling, the solution that has finally worked for me to sort Unicode Persian alphabets was using the Collator class:



            $col = new Collator('fa_IR');
            $col->asort($arr);
            var_dump($arr);


            I know the question is old but this might still be helping the new people getting here looking for an answer to this question.






            share|improve this answer













            well to get the available locales you can use



            print_r(ResourceBundle::getLocales(''));


            I had both 'fa' and 'fa_IR' available, however 'fa_IR' was still returning false so I used 'fa' to test it:



            setlocale(LC_ALL, 'fa');
            asort($arr, SORT_LOCALE_STRING);
            var_dump($arr);


            but this was not still sorting in the proper order for me...



            so after abit of more googling, the solution that has finally worked for me to sort Unicode Persian alphabets was using the Collator class:



            $col = new Collator('fa_IR');
            $col->asort($arr);
            var_dump($arr);


            I know the question is old but this might still be helping the new people getting here looking for an answer to this question.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 16 '17 at 7:51









            SaeedSaeed

            1,45911217




            1,45911217













            • As you can see, I haven't marked any answer as accepted because none of the solutions worked for me. Your workaround seems promising, I'll give it a try. Thanks a lot.

              – Farid Rn
              Oct 16 '17 at 16:01



















            • As you can see, I haven't marked any answer as accepted because none of the solutions worked for me. Your workaround seems promising, I'll give it a try. Thanks a lot.

              – Farid Rn
              Oct 16 '17 at 16:01

















            As you can see, I haven't marked any answer as accepted because none of the solutions worked for me. Your workaround seems promising, I'll give it a try. Thanks a lot.

            – Farid Rn
            Oct 16 '17 at 16:01





            As you can see, I haven't marked any answer as accepted because none of the solutions worked for me. Your workaround seems promising, I'll give it a try. Thanks a lot.

            – Farid Rn
            Oct 16 '17 at 16:01


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22796522%2fphp-array-sorting-and-compatibility-with-persian-alphabets%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud