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
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 N
s.
PS: It is much more better to use regular PHP array $arr[$i]
instead of surrogate array ${"Array".$i}
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.