Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 0.70 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I find a previously unknown pattern in an array using PHP?
  2. $arr=array(1,1,3,5,1,1,3,5,1,1,3,5,1,1,3,5);
  3. $p=array();
  4. for($i=0;$i<count($arr);$i++){
  5.     $tmp=$arr[$i].'';
  6.     for($j=$i;$j<count($arr);$j++){
  7.         $tmp.=','.$arr[$j];
  8.         if(isset($p[$tmp])){
  9.             //nope
  10.         }
  11.         else{
  12.             //nice try
  13.         }
  14.     }
  15.  
  16. }
  17. foreach($p as $key=>$val){
  18.     if($val>1)
  19.     echo "The patter: $key appeared $val times<br>";
  20. }
  21.        
  22. $arr = array(1,1,3,5,1,1,3,5,1,1,3,5,1,1,3,5,1,1,3,5);
  23. $data = implode('', $arr);  
  24. for($i=0; $i < count($arr)-1; $i++){
  25. $pattern .= $arr[$i];
  26. if (substr_count($data ,$pattern) !=1)
  27. echo $pattern . ' found '.substr_count($data ,$pattern). ' times<br />';
  28. }