Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create dynamic arrays and process it using php
    text
    copied!<p>I want to create a dynamic array and process the data inside the array. Example: </p> <p>a) If have <strong>2</strong> as digit input value, then it must create 2^2 = 4 arrays with 2 elements each. These elements are in form of binary (2 elements) => 00, 01, 10, 11</p> <pre><code>[pre] Array1[] = {0, 0}; Array2[] = {0, 1}; Array3[] = {1, 0}; Array4[] = {1, 1}; [/pre] </code></pre> <p>b) If i <strong>3</strong> as digit input value, then it must create 2^3 = 8 arrays with 3 elements each. These elements are in form of binary (3 elements) => 000, 001, 010, 011, 100, 101, 110, 111</p> <pre><code>[pre] Array1[] = {0, 0, 0}; Array2[] = {0, 0, 1}; Array3[] = {0, 1, 0}; Array4[] = {0, 1, 1}; Array5[] = {1, 0, 0}; Array6[] = {1, 0, 1}; Array7[] = {1, 1, 0}; Array8[] = {1, 1, 1}; [/pre] </code></pre> <p>Then i want to use this Array elements to calculate how many presence of "000" to "111" in a mysql table. The table has only 2 rows: id (auto_increment) and value (0/1). Example of searching for "101" in the table:</p> <pre><code>[pre] id | value ---------- 1 | 1 2 | 1 -- 3 | 0 | =&gt; (1) 4 | 1 -- 5 | 0 | =&gt; (2) 6 | 1 -- 7 | 0 8 | 0 9 | 1 10 | 1 .. | .. .. | .. 5000 | 1 -- 5001 | 0 | =&gt; (n) 5002 | 1 -- 5003 | 1 ... | ... [/pre] </code></pre> <p>A friend (Barmar) gave a solution in mysql to get how many "101" presence in the table (this is for : <strong>Array6[] = {1, 0, 1}; </strong> as example) , by using this code :</p> <pre><code>[pre] select count(*) match_count from TheTable t1 join TheTable t2 on t1.id+1 = t2.id join TheTable t3 on t1.id+2 = t3.id where t1.value = 1 and t2.value = 0 and t3.value = 1 [/pre] </code></pre> <p><strong>My Questions :</strong> <BR><strong><em>1) How to make the 8 arrays that hold the elements {0,0,0} ... {1,1,1} for an input = 3?</em></strong> <BR><strong><em>2) How to make the "FOR" routine for these 8 arrays to search for the pattern inside mysql table ? </em></strong>I tried this below, but the result was wrong ....</p> <pre><code>[pre] .... for ($i=1; $i&lt;=8; $i++) { // this is 2^3 = 8 arrays for ($k=0; $k&lt;3; $k++) { $element$k = Array$i[$k]; // get each array elements (3 elements in each array) } $result = select count(*) match_count from TheTable t1 join TheTable t2 on t1.id+1 = t2.id join TheTable t3 on t1.id+2 = t3.id where t1.value = $element$k and t2.value = $element$k and t3.value = $element$k; // these values must change for every array .... $query = mysql_query("INSERT INTO theResult(binary,presence) VALUES("'.$element$k.'","'.$result.'") "); } // end of for [/pre] </code></pre> <p>I expect to see this at mysql table "theResult" :</p> <pre><code>[pre] binary | presence -------------------- 000 | 21 001 | 18 010 | 32 011 | 11 100 | 44 101 | 17 110 | 8 111 | 25 [/pre] </code></pre> <p>But it doesn't happen like that ... :( Please help. Thank you</p>
 

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