利用PHP怎么获取字符流中不重复的字符

利用PHP怎么获取字符流中不重复的字符

今天就跟大家聊聊有关利用PHP怎么获取字符流中不重复的字符,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

使用索引数组

实现代码

<?php
global $result;
//Init module if you need
function Init(){
  global $result;
  $result = [];
}
//Insert one char from stringstream
function Insert($ch)
{
  global $result;
  // write code here
  if(isset($result[$ch])){
    $result[$ch]++;
  }else{
    $result[$ch] =1; 
  }
}
//return the first appearence once char in current stringstream
function FirstAppearingOnce()
{
  global $result;
  foreach($result as $k =>$v){
    if($v ==1){
      return $k;
    }
  }
  return "#";
}

看完上述内容,你们对利用PHP怎么获取字符流中不重复的字符有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。