Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your looking for how something like this works, this is how i acheived it with no php libraries using binary. </p> <pre><code>function search_get_combos($query){ $list = explode(" ", $query); $bits = count($list); //bits of binary number equal to number of words in query; //Convert decimal number to binary with set number of bits, and split into array $dec = 1; $binary = str_split(str_pad(decbin($dec), $bits, '0', STR_PAD_LEFT)); while($dec &lt; pow(2, $bits)) { //Each 'word' is linked to a bit of the binary number. //Whenever the bit is '1' its added to the current term. $curterm = ""; $i = 0; while($i &lt; ($bits)){ if($binary[$i] == 1) { $curterm .= $list[$i]." "; } $i++; } $terms[] = $curterm; //Count up by 1 $dec++; $binary = str_split(str_pad(decbin($dec), $bits, '0', STR_PAD_LEFT)); } return $terms; } </code></pre> <p>Note that this will return only unique combinations though, but can easily be extended to get every possible order of combinations so in your example this outputs:</p> <pre><code>Array ( [0] =&gt; fish [1] =&gt; dog [2] =&gt; dog fish [3] =&gt; cat [4] =&gt; cat fish [5] =&gt; cat dog [6] =&gt; cat dog fish ) </code></pre> <h1>Edit (More clarification)</h1> <h2>Basic theory</h2> <p>So firstly, binary numbers as you probably know are a string of 1's and 0's. The length of the number is the number of 'bits' it has, eg. the number <code>011001</code> has 6 bits (The numbers 25 in case you interested). Then if each bit of the number corresponds to one of the terms, each time it counts up, if the bit is 1, the term is included in the output, whereas if it's 0, it is ignored. So thats the basic theory of what's happening.</p> <h2>Delving into the code</h2> <p>PHP has no way of counting in binary, but you can convert decimals to binary. So this function actually counts up in decimal, and converts that to binary. But because the number of bits is important, as each term needs its own bit, you need to add leading 0's, so thats what this bit does: <code>str_pad(decbin($dec), $bits, '0', STR_PAD_LEFT)</code></p> <p>Now this function uses a while loop, but as the number of times it needs to loop changes depending on how many terms there are, a bit of maths needs to be done. If you have ever worked with binary, you will know that the maximum number you can make is the 2^n (where n is the number of bits).</p> <p>I think that should have covered all the confusing bits of the function, let me know if I've missed anything.</p> <h1>See what's happening</h1> <p>Use the following code to output the logic used, it may make a bit more sense seeing it this way!</p> <pre><code>function search_get_combos_demo($query){ $list = explode(" ", $query); $bits = count($list); $dec = 1; while($dec &lt; pow(2, $bits)) { $binary = str_split(str_pad(decbin($dec), $bits, '0', STR_PAD_LEFT)); $curterm = ""; $i = 0; while($i &lt; ($bits)){ if($binary[$i] == 1) { $curterm[] = $list[$i]." "; } $i++; } //-----DISPLAY PROCESS-----// echo "Iteration: $dec &lt;table cellpadding=\"5\" border=\"1\"&gt;&lt;tr&gt;"; foreach($binary as $b){ echo "&lt;td&gt;$b&lt;/td&gt;"; } echo "&lt;/tr&gt;&lt;tr&gt;"; foreach($list as $l){ echo "&lt;td&gt;$l&lt;/td&gt;"; } echo "&lt;/tr&gt;&lt;/table&gt;Output: "; foreach($curterm as $c){ echo $c." "; } echo "&lt;br&gt;&lt;br&gt;"; //-----END DISPLAY PROCESS-----// $terms[] = $curterm; $dec++; } return $terms; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload