dqkxo44488
2018-04-27 06:39
浏览 196
已采纳

php - 首先使用大写字母按字母顺序对数组进行排序的函数

I'm trying to sort an array alphabetically with uppercase letters first in the array

Example:

array(7) {
  ["H"]=>
  int(1)
  ["W"]=>
  int(1)
  ["e"]=>
  int(1)
  ["l"]=>
  int(3)
  ["o"]=>
  int(2)
  ["r"]=>
  int(1)
  ["d"]=>
  int(1)
}

My code doesn't sort with uppercase letters, only alphabetically

Here is my code:

function count_char($str) {
    $chars = str_split($str);
    $char_counter = Array();
    foreach($chars as $char) 
        if ((ord($char) >= 65 && ord($char) <= 90) || 
            (ord($char) >= 97 && ord($char) <= 122)) {
            if(!isset($char_counter[$char])) $char_counter[$char] = 1;
            else $char_counter[$char] += 1;
        }
    return $char_counter;
}

var_dump(count_char("Hello World"));

My desired output is $str, I would like alphabetizing the uppers, then alphabetizing the lowers

图片转代码服务由CSDN问答提供 功能建议

我正在尝试按字母顺序对数组中的大写字母进行排序

示例:

  array(7){
 [“H”] =&gt; 
 int(1)
 [“W”] =&gt;  
 int(1)
 [“e”] =&gt; 
 int(1)
 [“l”] =&gt; 
 int(3)
 [“o”] =&gt; 
  int(2)
 [“r”] =&gt; 
 int(1)
 [“d”] =&gt; 
 int(1)
} 
   \  n 
 

我的代码不按大写字母排序,只按字母顺序排序

这是我的代码:

  function count_char  ($ str){
 $ chars = str_split($ str); 
 $ char_counter = Array(); 
 foreach($ chars as $ char)
 if((ord($ char)&gt; = 65&amp;  ;&amp; ord($ char)&lt; = 90)|| 
(ord($ char)&gt; = 97&amp;&amp; ord($ char)&lt; = 122)){
 if(!isset(  $ char_counter [$ char]))$ char_counter [$ char] = 1; 
 else $ char_counter [$ char] + = 1; 
} 
返回$ char_counter; 
} 
 
var_dump(count_char(“  Hello World“)); 
   
 
 

我想要的输出是$ str,我好 d喜欢按字母顺序排列鞋面,然后按字母顺序降低

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyan8896 2018-04-27 06:53
    已采纳

    ksort() will do. You should only call ord() once and just store the result to minimize function calls. ...or better just call ctype_alpha() to ensure you are only storing letters. I recommend adding curly brackets for improved readability.

    Code: (Demo)

    function count_char($str) {
        $chars = str_split($str);
        $char_counter = array();
        foreach($chars as $char) {
            if (ctype_alpha($char)) {
                if (!isset($char_counter[$char])) {
                    $char_counter[$char] = 1;
                } else {
                    ++$char_counter[$char];
                }
            }
        }
        ksort($char_counter);
        return $char_counter;
    }
    
    var_dump(count_char("Hello World"));
    

    Output:

    array(7) {
      ["H"]=>
      int(1)
      ["W"]=>
      int(1)
      ["d"]=>
      int(1)
      ["e"]=>
      int(1)
      ["l"]=>
      int(3)
      ["o"]=>
      int(2)
      ["r"]=>
      int(1)
    }
    

    You could also condense things like this if you aren't scared off by regex:

    function count_char($str) {
        $letters = preg_split('~[^a-z]*~i', $str, -1, PREG_SPLIT_NO_EMPTY);
        if (!$letters) return [];
        $counts = array_count_values($letters);
        ksort($counters);
        return $counters;
    }
    
    var_dump(count_char("Hello World"));
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanpo6079 2018-04-27 06:51

    Personally I would do it like:

    <?php
    $str = "Hello World";
    
    // split the string (ignoring spaces)
    $array = str_split(str_replace(' ', '', $str), 1);
    
    // count the chars
    $array = array_count_values($array);
    
    // sort naturally
    array_multisort(array_keys($array), SORT_NATURAL, $array);
    
    print_r($array);
    

    https://3v4l.org/aZqRb

    Result:

    Array
    (
        [H] => 1
        [W] => 1
        [d] => 1
        [e] => 1
        [l] => 3
        [o] => 2
        [r] => 1
    )
    

    Edit: If you want to sort by value and then by key:

    <?php
    $str = "Hello World";
    
    // split the string (ignoring spaces)
    $array = str_split(str_replace(' ', '', $str), 1);
    
    // count the chars
    $array = array_count_values($array);
    
    // get the keys
    $keys = array_keys($array);
    
    // sort my keys
    array_multisort($array, $keys);
    
    // combine sorted keys with array
    $array = array_combine($keys, $array);
    
    print_r($array);
    

    https://3v4l.org/pfEin

    Result:

    Array
    (
        [H] => 1
        [W] => 1
        [d] => 1
        [e] => 1
        [r] => 1
        [o] => 2
        [l] => 3
    )
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题