Advertisement
Guest User

sparse arrays

a guest
Oct 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. <?php
  2.  
  3.     $string = ['aca','aca','aca', 'aab', 'aaa', 'aa'];
  4.     $query = ['aaa','aa','abba', 'aas'];
  5.     $result = array_fill_keys($query, 0);
  6.  
  7.     //show String
  8.     echo "String = ".count($string)."<br>";
  9.     foreach ($string as $val){
  10.         echo $val."<br>";
  11.     };
  12.     echo "<hr>";
  13.    
  14.     //Show query
  15.     echo "Query = ".count($query)."<br>";
  16.     foreach ($query as $val){
  17.         echo $val."<br>";
  18.     };
  19.     echo "<hr>";
  20.  
  21.     //Process
  22.     for($i=0; $i<count($string); $i++){
  23.         for ($j=0; $j< count($query); $j++){
  24.             if($query[$j]==$string[$i]){
  25.                 $result[$query[$j]] += 1;
  26.             }
  27.             else {
  28.                 $result[$query[$j]] += 0;
  29.             }  
  30.         }
  31.     }
  32.  
  33.     echo "Result = <br>";
  34.     foreach ($result as $val){
  35.         echo $val."  ";
  36.     };
  37.  
  38. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement