I check the code and realized that it can't show repetitive numbers
MY CODE
<?php
/* Designated level for each exp
Level 2 - 23 exp
Level 3 - 34 exp
Level 4 - 45 exp
Level 5 - 56 exp
Level 6 - 68 exp
Level 7 - 79 exp
Level 8 - 90 exp
Level 9 - 101 exp
Level 10 - 112 exp
Level 11 - 123 exp
Level 12 - 134 exp
Level 13 - 145 exp
Level 14 - 156 exp
Level 15 - 168 exp
Level 16 - 179 exp
*/
$limit = 100000-99370;
// Level
$arrlevel = array ('Level 2','Level 3','Level 4','Level 5','Level 6','Level 7','Level 8','Level 9','Level 10','Level 11','Level 12','Level 13','Level 14','Level 15','Level 16');
// Exp
$array = array (23,34,45,56,68,79,90,101,112,123,134,145,156,168,179);
$array = array_filter($array, function($var) use ($limit) {
return ($var <= $limit);
});
$num = count($array);
$total = pow(2, $num);
$out = array();
for ($i = 0; $i < $total; $i++) {
$comb = array();
for ($j = 0; $j < $num; $j++) {
// is bit $j set in $i?
if (pow(2, $j) & $i){
$comb[] = $array[$j];
}
}
if (array_sum($comb) == $limit)
{
$out[] = $comb;
}
}
array_multisort(array_map('count', $out), SORT_ASC, $out);
$out = array_unique($out, SORT_REGULAR);
$m = 1;
$mapper = [
23 => "Level 2",
34 => "Level 3",
45 => "Level 4",
56 => "Level 5",
68 => "Level 6",
79 => "Level 7",
90 => "Level 8",
101 => "Level 9",
112 => "Level 10",
123 => "Level 11",
134 => "Level 12",
145 => "Level 13",
156 => "Level 14",
168 => "Level 15",
179 => "Level 16",
];
foreach($out as $result)
echo "<b>Possible Answer ". $m++. " : </b><br> " .implode(' , ', array_map(function($x) use ($mapper) {
return $mapper[$x] . " - " . $x;
}, $result))."
<br><br>";
My Input and Ouput
If i input 99318
the output is like this
Possible Answer 1 :
Level 10 - 112 , Level 11 - 123 , Level 12 - 134 , Level 13 - 145 , Level 15 - 168
I want to generate also the repetitive numbers too
But it cannot show some repetitive numbers answer like this
Possible Answer :
Level 4 - 45 , Level 10 - 112 , Level 11 - 123 , Level 11 - 123 , Level 12 - 134 , Level 13 - 145
You can see there's two Level 11 - 123
I want the ouput like this
Possible Answer :
Level 4 - 45 , Level 10 - 112 , Level 11 (x2) - 246 , Level 12 - 134 , Level 13 - 145
I want to group all repititive numbers and sum up them all
One option to get your result is to pass another value to array_map with the result of array_count_values.
Then inside the mapping you can determine to show the count for the number based on the index like $countValues[$x] just as for the mapper.
For example
foreach($out as $result) {
$countValues = array_count_values($result);
echo "<b>Possible Answer " . $m++ . " : </b><br> " . implode(' , ',
array_map(function ($x) use ($mapper, $countValues) {
$strCount = $countValues[$x] > 1 ? " (" . $countValues[$x] . ")" : "";
return $mapper[$x] . $strCount . " - " . $x;
}, array_unique($result))) . "
<br><br>";
}
That will give you a result like
Possible Answer 1 :
Level 2 - 23 , Level 6 - 68 , Level 7 (x2) - 79 , Level 9 - 101 , Level 10 - 112 , Level 15 - 168
Php demo with as a test a duplicate value 79 for $array
Related
I want to combine the two arrays in my results
MY CODE
<?php
/* Designated level for each exp
Level 2 - 23 exp
Level 3 - 34 exp
Level 4 - 45 exp
Level 5 - 56 exp
Level 6 - 68 exp
Level 7 - 79 exp
Level 8 - 90 exp
Level 9 - 101 exp
Level 10 - 112 exp
Level 11 - 123 exp
Level 12 - 134 exp
Level 13 - 145 exp
Level 14 - 156 exp
Level 15 - 168 exp
Level 16 - 179 exp
*/
$limit = 100000-99318;
// Level
$arrlevel = array ('Level 2','Level 3','Level 4','Level 5','Level 6','Level 7','Level 8','Level 9','Level 10','Level 11','Level 12','Level 13','Level 14','Level 15','Level 16');
// Exp
$array = array (23,34,45,56,68,79,90,101,112,123,134,145,156,168,179);
$array = array_filter($array, function($var) use ($limit) {
return ($var <= $limit);
});
$num = count($array);
$total = pow(2, $num);
$out = array();
for ($i = 0; $i < $total; $i++) {
$comb = array();
for ($j = 0; $j < $num; $j++) {
// is bit $j set in $i?
if (pow(2, $j) & $i){
$comb[] = $array[$j];
}
}
if (array_sum($comb) == $limit)
{
$out[] = $comb;
}
}
array_multisort(array_map('count', $out), SORT_ASC, $out);
$out = array_unique($out, SORT_REGULAR);
$m = 1;
foreach($out as $result)
echo "<b>Possible Answer ". $m++. " : </b> " .implode(', ', $result)."
<br><br>";
?>
Output:
Possible Answer 1 :
23, 34, 45, 68, 79, 90, 112, 179
Possible Answer 2 :
23, 34, 45, 68, 79, 90, 123, 168
Possible Answer 3 :
23, 34, 45, 68, 79, 101, 112, 168
I want the ouput like this
Possible Answer 1 :
Level 2 - 23 | Level 3 - 34 | Level 4 - 45 | Level 6 - 68 | Level 7 - 79 | Level 8 - 90 | Level 10 - 112 | Level 16 - 179
-----------------------------------------
I want to combine the two arrays
(This program is about finding all combination to reach the result [Subset sum program])
You might use a mapper for the entries of Level n and the experience.
Then for the $result array in the loop you could use array_map and for each item that you are mapping use the value as the key for the $mapper array and implode using |
That will format all the possible answers in this format:
Possible Answer 1 :
Level 3 - 34 | Level 13 - 145 | Level 14 - 156 | Level 15 - 168 | Level 16 - 179
For example
$mapper = [
23 => "Level 2",
34 => "Level 3",
45 => "Level 4",
56 => "Level 5",
68 => "Level 6",
79 => "Level 7",
90 => "Level 8",
101 => "Level 9",
112 => "Level 10",
123 => "Level 11",
134 => "Level 12",
145 => "Level 13",
156 => "Level 14",
168 => "Level 15",
179 => "Level 16",
];
foreach($out as $result)
echo "<b>Possible Answer ". $m++. " : </b> " .implode(' | ', array_map(function($x) use ($mapper) {
return $mapper[$x] . " - " . $x;
}, $result))."
<br><br>";
Php demo
Just do it and do sort
$combine=array_combine($arrlevel,$array);
Brief idea of what I'm doing is taking lines of writing that are always in same format which I'm using
$result = $_POST['result'];
$result = explode(" ",$result);
foreach($result as $k => $v){
${"var".$k} = $v;
}
$var11 = trim($var11, "(");
$var18 = trim($var18, "(");
$var25 = trim($var25, "(");
$var32 = trim($var32, "(");
and then picking out the variables I need to insert into my database
The format of the text string
[18:20:23] Neptune> At [18:20] on Table 40: SassyNClassy G( 4 - 288 ) TipMeGood B( 1 - 174 ) StandUpMan Y( 0 - 23 ) Ruby2017 R( 0 - 10 )
varaibles below
Which echos out into these variables
$var1 = [18:20:23]
$var8 = 40:
$var9 = SassyNClassy
$var11 = G
$var12 = 4
$var14 = 288
$var16 = TipMeGood
$var18 = B
$var19 = 1
$var21 = 174
$var23 = StandUpMan
$var25 = Y
$var26 = 0
$var28 = 23
$var30 = Ruby2017
$var32 = R
$var33 = 0
$var35 = 10
Now what I am looking for is if it is possible how would I go about linking variables Green (9 11 12 14) blue (16 18 19 21) yellow (23 25 26 28) red (30 32 33 35)
and reordering them so that its blue green red yellow keeping in mind the order of colours changes based on the string input
I will be doing a input into my table which I can do but I need to have it all in same order for other php code I'm in process of writing to add the 3rd variable of (blue and green) and of (red and yellow) as well as the scores of those 2 teams
UPDATED
Here are a few examples of what are posted via
$result = $_POST['result'];
[18:20:23] Neptune> At [18:20] on Table 40: SassyNClassy G( 4 - 288 ) TipMeGood B( 1 - 174 ) StandUpMan Y( 0 - 23 ) Ruby2017 R( 0 - 10 )
What it should be in order
TipMeGood B 1 174 SassyNClassy G 4 288 StandUpMan Y 0 23 Ruby2017 R 0 10
[18:21:50] Neptune> At [18:21] on Table 35: oldstray Y( 4 - 288 ) MeanyBritches R( 2 - 188 ) racing_2425 B( 0 - 60 ) ladyhawk__ G( 0 - 20 )
What it should be in order
racing_2425 B 0 60 ladyhawk__ G 0 20 MeanyBritches R 2 - 188 oldstray Y 4 288
[18:22:01] Neptune> At [18:22] on Table 25: junesnugglebug R( 4 - 288 ) WhamBam_Booty G( 2 - 213 ) Misfitting B( 2 - 178 ) kfallsgrl Y( 0 - 137 )
What it should be in order
Misfitting B 2 178 WhamBam_Booty G 2 213 junesnugglebug R 4 288 kfallsgrl Y 0 137
I have a module in my projects its about the finding free places in the classroom timetable. I am taking variables without problem from database. I need an array which is returns for all classroom which is how many classroom i have in DB. This classrooms also has variable time period 1 till 12 and each period has duration for example if i have in 4th period 3 duration it should write 1 2 3 x x x 7 8 9 10 11 12 and if i have another course in 9th period 2 duration it should be 1 2 3 x x x 7 8 x x 11 12. I did it in 1D array if i give the class_no in query. but it should do it more than a classroom. it shows just numbers 1..12 in 1 line.
$dayy = $_GET['src_day0'];
$drt = $_GET['src_duration0'];
$tm = $_GET['src_time0'];
$faculty_id = $_SESSION['faculty_id'];
$scale = "select DISTINCT t.class_no,t.time,t.duration from ttable t,class c where
day='$dayy' AND (t.faculty='$faculty_id' OR c.faculty='$faculty_id')";
$result = $conn->query($scale);
$x = 1;
while ($rows = $result->fetch_assoc()) {
$class = $rows['class_no'];
$arr = array(
array($x => "$class"),
array(
1 => " 1 ", 2 => " 2 ", 3 => " 3 ", 4 => " 4 ", 5 => " 5 ", 6 => " 6 ",
7 => " 7 ", 8 => " 8 ", 9 => " 9 ", 10 => " 10 ", 11 => " 11 ", 12 => " 12 ",
));
$x++;
}
while ($rows = $result->fetch_assoc()) {
$time = $rows['time'];
$duration = $rows['duration'];
$result1 = ($time + $duration);
for($j=1;$j<$x;$j++)
for ($i = $time; $i < $result1; $i++)
$arr[$j][$i] = "x1";
}
}
for ($i = 1; $i < $x; $i++) {
for ($j = 1; $j < 13; $j++)
echo $arr[$i][$j];
echo "</br>";
In fact you missbuilded your array :)
If your looking your code:
$class = $rows['class_no'];
$arr = array(
array($x => "$class"),
array(
1 => " 1 ", 2 => " 2 ", 3 => " 3 ", 4 => " 4 ", 5 => " 5 ", 6 => " 6 ",
7 => " 7 ", 8 => " 8 ", 9 => " 9 ", 10 => " 10 ", 11 => " 11 ", 12 => " 12 ",
));
$x++;
For each iteration of the loop, you overwrite the array with a new value.
In fact, you need to create a new data and append it on the array.
A way to do it, is the following process:
$arr = [];
while ($rows = $result->fetch_assoc()) {
$arr[ $rows['class_no'] ] = [1 => ' 1 ', 2 => ' 2 ', /*...*/];
}
// free memory if needed
while ($rows = $result->fetch_assoc()) {
for( $i = $rows['time'] ; $i < $rows['time'] + $rows['duration'] ; $i++ ) {
$arr[ $rows['class_no'] ][ $i ] = 'x';
}
}
Here, you create an empty variable $arr of array type. Then, for each data on the loop, you created a new line (for each class_no) with the default values.
On the second loop, you iterate again on the results, but here, you take each array data by class_no and change the correct time value to x
Each number has a corresponding value with it. There are many numbers which I can demonstate in a table here with their appropriate values:
[N] [V] N=Number V=Value
2 19
4 19
6 19
8 21
10 21
12 22
14 23
16 23
18 23
20 33
22 37
24 42
26 45
28 48
30 50
32 55
34 61
36 66
38 72
40 78
42 155
44 179
46 202
48 233
50 360
There is a process that a user will go through where they go from Number x to Number y. The values inbetween those numbers need to get added together. So for example, let's say a user goes from 16 to 38:
[N] [V] N=Number V=Value
2 19
4 19
6 19
8 21
10 21
12 22
14 23
[16][23]--
18 23 |
20 33 |
22 37 |
24 42 |
26 45 |
28 48 |---- All of these values get added together
30 50 |
32 55 |
34 61 |
36 66 |
[38][72]--
40 78
42 155
44 179
46 202
48 233
50 360
So the users total value would equal be:
23 + 23 + 33 + 37 + 42 + 45 + 48 + 50 + 55 + 61 + 66 + 72
Total Value = 555
The problem is, is that I have no idea how I to put this together in code. Like how to assign these values to their specific number and how to add those specific values together to get me a result. In PHP I simply do not know where to begin with this.
Also, the approximate values from the numbers can be represented by this equation:
v = 11.218e^(0.057n)
I would imagine this would be useful in making this whole process easier but I am still not sure how to go about implementing all of this. Any help would be very much apprieciated!
Put each each number with it's corresponding value into an array making number as key and value pair like this.
<?php
$arr = array(
2=> 19,
4=> 19,
6=> 19,
8=> 21,
10=> 21,
12=> 22,
14=> 23,
16=> 23,
18=> 23,
20=> 33,
22=> 37,
24=> 42,
26=> 45,
28=> 48,
30=> 50,
32=> 55,
34=> 61,
36=> 66,
38=> 72,
40=> 78,
42=> 155,
44=> 179,
46=> 202,
48=> 233,
50=> 360,
);
?>
Loop array with foreach loop like this
<?php
$sum = 0;
foreach($arr as $k => $v) {
if($k >= 16 && $k <= 38)
$sum += $v;
}
?>
There is another way using for loop statement, put both number in two separate array ($n and $v). Iterate the loop of the first array($n) and find the value from second array($v) through the index number of first array. But both array count should have same.
Example-
<?php
$n = array(2,4,6,8,10,12,14,16,18,20);
$v = array(19,19,19,21,21,22,23,23,23,33);
$sum = 0;
for($i=0, $i<count($n); $i++) {
if($n[$i] >= 16 && $n[$i] <= 38)
$sum += $v[$i];
}
?>
You would put your number and value pairs into an key / value array. So a shortened version of your test data would look like this:
$myDataStore = array(
"2" => "19",
"4" => "19",
"6" => "19",
"8" => "21",
"10" => "21",
"12" => "22",
"14" => "23",
"16" => "23",
"18" => "23",
"20" => "23"
);
Now you need a function to calculate your sum given a range as defined by starting and ending numbers.
function getRangeTotal($array, $startNumber, $endNumber){
$total = 0;
foreach($array as $key => $value){
if($key >= $startNumber && $key <= $endNumber){
$total = $total + $value;
}
}
return $total;
}
If you run the above function
getRangeTotal($myDataStore, 6, 12);
You'll get 83
Here is how you can do this using foreach
// first store you data to an array.
$kv = array(
2 => 19, 4 => 19, 6 => 19, 8 => 21,
10 => 21, 12 => 22, 14 => 23, 16 => 23,
18 => 23, 20 => 33, 22 => 37, 24 => 42,
26 => 45, 28 => 48, 30 => 50, 32 => 55,
34 => 61, 36 => 66, 38 => 72, 40 => 78,
42 => 155, 44 => 179, 46 => 202,48 => 233,
50 => 360
);
$start = 16;
$end = 32;
$sum = 0; //variable to keep sum
$n = 0; //variable to keep count
//loop through the array
foreach ($kv as $k => $v){
if ($k >=$start && $k <= $end){ //if key is in your range
$sum += $v; //add value to sum
$n ++; // increment count
}
}
$v = 11.218*pow(M_E,0.057*$n); //calculate the approximate values
echo "$sum\n$v\n";
Also refer to : pow and Predefined Constants
Is it possible to convert specific text file content into a php array ?
For example:
Text file
//group
1
// first id other values
1 5 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
//group
2
// first id other values
1 3 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
Return php array
1 => array(
1 => array(5, 7, 3, 83, 83, 83, 1),
2 => array...
),
2 => array(
1 => array(3, ...),
....
and so on, until end then next number group, and also ignore comments lines // or #
I've got another solution:
<?php
header('Content-type: text/plain');
$string = "//group
1
// first id other values
1 5 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
//group
2
// first id other values
1 3 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 2
end";
$string = preg_replace('/[^0-9 \n]/','',$string);
$array = array_filter(explode("\n", $string));
$temp_array = array();
$new_array = array();
$index = -1;
foreach($array as $key => $arr){
if(strlen(trim($arr)) == 1 && intval($arr) > 0){
$index = intval($arr);
}
else if(strlen(trim($arr)) > 4){
$temp_array = array_values(array_filter(explode(" ", $arr)));
$temp_index = $temp_array[0];
unset($temp_array[0]);
$new_array[$index][$temp_index] = $temp_array;
}
}
print_r(array_filter($new_array));
?>
Solved...
Text file
// group 1
1
1 4 3 "ssssssss"
end
// group 2
2
1 5 4 "ssssssss s"
end
// group 3
3
1 6 5 "ssssssss ss"
end
php convert code
$list = array();
$handle = fopen('Item.txt', 'r');
while(($line = fgets($handle)) !== false)
{
// skip comments
if(preg_match('!^//!', $line) || preg_match('/#/', $line))
{
continue;
}
// replace all spaces
$line = preg_replace('~"[^"]*"(*SKIP)(*F)|\s+~', ',', $line);
// skip blanks
if($line[0] == ',')
{
continue;
}
// define group
if(substr_count($line, ',') == 1 || substr_count($line, 'end') == 1)
{
if(substr_count($line, 'end') == 0)
{
$key = str_replace(',', '', $line);
}
continue;
}
// remove last comma
if(substr($line, -1) == ',')
{
$line = substr($line, 0, -1);
}
$arguments = explode(',', $line);
$id = $arguments[0];
unset($arguments[0]);
$list[$key][$id] = $arguments;
}
fclose($handle);
print_r($list);
output http://prntscr.com/6emxpq