2

I want to create a dynamic array and process the data inside the array. Example:

a) If have 2 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

[pre]
Array1[] = {0, 0};
Array2[] = {0, 1};
Array3[] = {1, 0};
Array4[] = {1, 1};
[/pre]

b) If i 3 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

[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]

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:

[pre]
id | value
----------
1  |   1
2  |   1   --
3  |   0    |  => (1)
4  |   1   --
5  |   0     | => (2)
6  |   1   -- 
7  |   0
8  |   0
9  |   1
10 |   1
.. |   ..
.. |   ..
5000 |  1  --
5001 |  0    |  => (n)
5002 |  1  --
5003 |  1
...  |  ...
[/pre]

A friend (Barmar) gave a solution in mysql to get how many "101" presence in the table (this is for : Array6[] = {1, 0, 1}; as example) , by using this 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]

My Questions :
1) How to make the 8 arrays that hold the elements {0,0,0} ... {1,1,1} for an input = 3?
2) How to make the "FOR" routine for these 8 arrays to search for the pattern inside mysql table ? I tried this below, but the result was wrong ....

[pre]
....
for ($i=1; $i<=8; $i++) {    // this is 2^3 = 8 arrays
  for ($k=0; $k<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]

I expect to see this at mysql table "theResult" :

[pre]
binary  | presence
--------------------
000     | 21
001     | 18
010     | 32
011     | 11
100     | 44
101     | 17
110     | 8
111     | 25
[/pre]

But it doesn't happen like that ... :( Please help. Thank you

1
  • 1
    You're mixing php with SQL... Commented Sep 8, 2013 at 17:26

1 Answer 1

1

The code for your exotic 1st problem is:

$N = 3;

for($i = 0; $i < pow(2,$N); $i++)
    ${"Array".$i} = str_split(str_pad(decbin($i), $N, '0', STR_PAD_LEFT));

Now $Array1, $Array2, ..., $Array8 consist of desired subarrays with binary values.

For example, value for $Array5 is

array(3) { [0]=> string(1) "1" [1]=> string(1) "0" [2]=> string(1) "1" }

The code is universal for natural Ns.

P.S.: It is much more better to use regular PHP array $arr[$i] instead of surrogate array ${"Array".$i}.

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you zavg. Can you give me a clue how to get the array elements? for ($i=0; $i< pow(2,$N), $i++) { for ($k=0; $k<$N; $k++) { $getElement.$k = $Array.$i[$k]; } $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 = $getElement.$k and t2.value = $getElement.$k and t3.value = $getElement.$k; // wrong here ? $getElement.$k will hold the same value ? ...... }
Hi zavg, i think i can use "Foreach" instead of "for" routine in mysql query .... thank you
Hi zavg, i tried at LAMP: print("<table><tr><td colspan=6><B>Result</B></td></tr>"); $N=3; for($i = 0; $i < pow(2,$N); $i++) { ${"myArray".$i} = str_split(str_pad(decbin($i), $N, '0', STR_PAD_LEFT)); echo('<tr><td colspan=6><B>3digit myArray: '.${"myArray".$i}.' </B></td></tr>'); } print("</table>"); And as result i got this : 3digit myArray: Array 3digit myArray: Array 3digit myArray: Array 3digit myArray: Array 3digit myArray: Array 3digit myArray: Array 3digit myArray: Array 3digit myArray: Array What is wrong?
@user2758001 To access items it is muxh more conveniebt to use just regurar PHP array $myArray[$i] instead of your suroggate array ${"Array".$i}
You can not just print arrays with print. You should iterate through its values in loop and print each item separately.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.