[PHP] array_count_value

php array_count_value str_split

  • 傳入一字串,回傳各字元的統計數量陣列
  • orderedCount("abracadabra") == [['a', 5], ['b', 2], ['r', 2], ['c', 1], ['d', 1]]
  • 利用function array_count_values
  • function orderedCount($text) {
      $textArray = str_split($text);
      $countArray = array_count_values($textArray);
      $result = [];
      foreach($countArray as $key => $value) {
        $result[] = [$key, $value];
      }
      return $result;
    }

     

  • end