PHP Gradual calculate - php

I have a problem with gradually calculating a value in PHP.
This is just an example. A user can level up, and when he reaches level 5 he unlocks the ability to play missions. The difficulty of a new generated mission is defined by a number: from 1 that is an easy mission to 10 that is a very difficult mission. See this table:
o 5: 1, 1, 2, 2, 3, 3, 3, 3, 3, 3 and so on
o 6: 1, 2, 2, 2, 3, 3, 3, 3, 3, 3
o 7: 2, 2, 2, 3, 3, 3, 3, 3, 3, 3
o 8: 2, 2, 3, 3, 4, 4, 4, 4, 4, 4
o 9: 2, 3, 3, 4, 4, 4, 4, 4, 4, 4
o 10: 3, 3, 4, 4, 5, 5, 5, 5, 5, 5
o 11: 3, 4, 4, 5, 6, 6, 6, 6, 6, 6
o 12: 4, 4, 5, 6, 7, 7, 7, 7, 7, 7
o 13: 4, 5, 6, 7, 8, 8, 8, 8, 8, 8
o 14: 5, 6, 7, 8, 9, 9, 9, 9, 9, 9
o 15: 6, 7, 8, 9, 10, 10, 10, 10, 10, 10
o 16: 7, 8, 9, 10, 10, 10, 10, 10, 10, 10
o 17: 8, 9, 10, 10, 10, 10, 10, 10, 10, 10
o 18: 9, 10, 10, 10, 10, 10, 10, 10, 10, 10
o 19: 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
o 20: same as 19
This means: in level 5 the first mission is very easy (difficulty 1), the second one too, and the third mission is difficulty 2. And when the player is level 18: the first mission has a difficulty of 9 and the second and following missions have a difficulty of 10.
But: how to generate the difficulty of the missions?
I have two variables:
<?php
$current_player_level
$current_mission_in_level
?>
So:
<?php
$current_player_level=8;
$current_mission_in_level=3;
//This should return: 3
?>
So I need a function that returns the difficulty when the two variables above are provided.
I don't have any code because I don't know how to begin.

I'd suggest storing the difficulties in a 2-dimensional array (also called a "matrix" sometimes). The code would look like so:
$matrix_val = array(5 => array(1, 1, 2, 2, 3),
6 => array(1, 2, 2, 2, 3),
7 => array(2, 2, 2, 3, 3),
8 => array(2, 2, 3, 3, 4),
9 => array(2, 3, 3, 4, 4),
10 => array(3, 3, 4, 4, 5),
11 => array(3, 4, 4, 5, 6),
12 => array(4, 4, 5, 6, 7),
13 => array(4, 5, 6, 7, 8),
14 => array(5, 6, 7, 8, 9),
15 => array(6, 7, 8, 9, 10),
16 => array(7, 8, 9, 10, 10),
17 => array(8, 9, 10, 10, 10),
18 => array(9, 10, 10, 10, 10),
19 => array(10, 10, 10, 10, 10));
Then all you have to do is:
$difficulty = $matrix_val[$current_player_level][$current_mission_in_level];
NOTE: The "current mission in level" assumes 0 is the first index. So the third mission in level 13 would be referenced as $matrix_val[13][2] (notice [2]). If you want to shift this you can either add individual keys to each level, or simply "pad" a meaningless entry at the start of each inner array. (If this is confusing, let me know and I'll expand.)
For reference, check out the official Array PHP documentation, then you might want to read up on 2-dimensional arrays, too.

You should use arrays.
You can use 2D array (like a table) to store your levels and difficulties, and then get the right value according to the position you want.
For example:
$table = array(
5 => array(1, 1, 2, 2, 3),
6 => array(1, 2, 2, 3, 3),
7 => array(2, 2, 3, 3, 4)
);
I'm not showing the all array here.
So if you want to know for example the difficuly, you can use:
$current_difficulty = $table[$current_player_level][$current_mission_in_level];
The is no much math here, just knowing arrays.

// Filling 2D array with linear gradient from left top to right bottom
// There are some math for this. But i'm not sure it is needed
$MIN = 5.0; // min difficalty
$MAX = 9.0; // max difficalty
$M = 15.0; // Level legnth - 1
$N = 7.0; // Levels count -1
$res = array();
for($y = 0; $y <= $N; $y++)
for($x = 0; $x <= $M; $x++)
{
$y1 = ($N*$y-$M*$x+$M*$M)*$N/($M*$M+$N*$N);
$res[$N-$y][$x] = round($MAX - $y1/$N*($MAX-$MIN));
}
for($i=0; $i <= $N; $i++) {
$line = $res[$i];
echo implode(' ', $line)."\n";
}
Output:
5 5 5 6 6 6 6 7 7 7 7 7 8 8 8 8
5 5 6 6 6 6 6 7 7 7 7 8 8 8 8 8
5 5 6 6 6 6 7 7 7 7 7 8 8 8 8 8
5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 9
5 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9
6 6 6 6 6 7 7 7 7 7 8 8 8 8 9 9
6 6 6 6 6 7 7 7 7 8 8 8 8 8 9 9
6 6 6 6 7 7 7 7 7 8 8 8 8 9 9 9

Related

Find Missing value between 2 arrays in PHP? [duplicate]

This question already has answers here:
difference between two arrays
(7 answers)
Closed 5 years ago.
i am trying to find the missing values between 2 arrays...
$in_id = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$out_id = array(0, 1, 3, 5, 6, 7, 8, 9, 10, 11);
in 2nd variable $out_id missing values 2, 4, 12 I want these values...
Result: e.g
$missing_values = 2,4,12
How about array_diff,
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
Output
Array
(
[1] => blue
)
You can use array_diff() for getting difference in array
<?php
$in_id = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$out_id = array(0, 1, 3, 5, 6, 7, 8, 9, 10, 11);
print_r(array_diff($in_id, $out_id))
?>
Array
(
[2] => 2
[4] => 4
[12] => 12
)

How to right align numbers from an array in php?

I have a problem with aligning numbers from a multidimensional array. I want to print the following result:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
And I want all of the numbers to be aligned with the second digit of the next rows. However my result is that:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
I did this in C# by using:
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write("{0,4}", matrix[row, col]);
}
But how can I receive this result in PHP?
You can use str_pad
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
$test = '';
foreach ($arr as $key => $value) {
if ($key % 4 == 0) {
$test .= "\n";
}
$test .= str_pad($value, 4, ' ', STR_PAD_LEFT);
}
echo "<pre>$test</pre>";
The result would be:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Create an array from range elements with a base array

I have an array like this
$baseArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
So i want to make two arrays of range
$arrayOne = from index 5 - 10
$arrayTwo = from index 15 - 20
How can I split the $baseArray to make arrays like that in php ?
There is function array_slice
$baseArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
$arrayOne = array_slice($baseArray, 5, 10);
$arrayTwo = array_slice($baseArray, 15, 20);
This should work for you:
(Only works if you have an index with numbers start's with 0)
$baseArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
$arrayOne = array();
$arrayTwo = array();
foreach($baseArray as $k => $v) {
if($k >= 5 && $k <= 10)
$arrayOne[] = $v;
elseif($k >= 15 && $k <= 20)
$arrayTwo[] = $v;
}
print_r($arrayOne);
print_r($arrayTwo);
Output:
arrayOne:
Array
(
[0] => 6
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
)
arrayTwo:
Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 19
[4] => 20
)
Can try using for() & array_slice(). Example:
$baseArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
$newAr = array();
for($i = 5; $i < count($baseArray); $i += 10){
$newAr[] = array_slice($baseArray, $i, 5);
}
print '<pre>';
print_r($newAr);
print '</pre>';

How do I remove an element from an array and insert into another position in PHP?

For example suppose I have
$input = array(0, 1, 2, 3, 4, 5, 6, 7);
How would I remove element 5 and insert at position 2 leaving me with
0, 1, 5, 2, 3, 4, 6, 7
$input = array(0, 1, 2, 3, 4, 5, 6, 7);
array_splice($input, 2, 0, array($input[5])); //Place the a new array in the 3rd place of array
unset($input[6]); //remove the fifth element
array_splice($input, 0, 0); //to update the indexes
echo "<pre>".print_r($input,1)."</pre>"; //to view the array
Method without the need to unset and rearrange the index
$input = array(0, 1, 2, 3, 4, 5, 6, 7);
array_splice($input, 2, 0, array_splice($input,5,1));
Output
Array
(
[0] => 0
[1] => 1
[2] => 5
[3] => 2
[4] => 4
[5] => 5
[6] => 6
[7] => 7
)
$input = array(0, 1, 2, 3, 4, 5, 6, 7);
$moved = array_splice($input, 5, 1);
array_splice($input, 2, 0, $moved);
You should take a look at the function array_splice:
http://www.php.net/manual/en/function.array-splice.php
That will remove elements from an array and also insert.
array_splice(); will do it for you http://www.php.net/manual/fr/function.array-splice.php
EDIT :
array_splice(your_array,position_where_you_want_to_replace_your_number,1,replacement_number);
Never used it before, may I've made a mistake above, but it's a start, insn't it? :)
EDIT2 : above, you'll have to remove the second number which is still here with my function.

Creating matches with 3 arrays

I have an issue with an application that I'm developing for a car pooling program in my company (the process is kind of complex). What I want to do is the follwoing:
I have 3 teams of 3 people, each team has a unique member id for instance:
Group 1 = (1,2,3,4)
Group 2 = (5,6,7,8)
Group 3 = (9,10,11,12)
The idea is to make as many combinations as possible of 2 members (I think it is at least 8 per member) without matching with someone from the same group.
For example
1-5
1-6
1-7
1-8
1-9
1-10
1-11
1-12
2-5
2-6
2-7
2-8
2-9
...
and so on
This is a code snippet (it might not have sense with what I want to achieve but I'm a junior programmer)
<?php
$numberSet = array( range(1,4),
range(5,8),
range(9,12)
);
$sizeofArray=count($numberSet);
for ($i=0; $i<$sizeofArray; $i++){
for ($j=0; $j<count($numberSet[$i]); $j++){
for ($k=0; $k<count($numberSet[$i]); $k++){
echo $numberSet[$i][$j] . "<br>";
}
}
}
?>
If you clear up what it is you actually want to achieve, it maybe a little more help, but to be going on with, here is one way to get all the matches for a member of one group, without matching it to anyone from its own group - I will assume you plan on having multiple ID's and not a simple 1234, 5678, 9 10 11 12 in your working set:
// Build an example array:
$numberSet = array( range(1,4),
range(5,8),
range(9,12) );
// The function will return an array of matches when passed the array and the ID:
function findCombos($id, $set)
{
// Store the matches found:
$matches = array();
// Loop through each array in the multidimensional array which was passed:
foreach ($set as $group)
{
// Make sure the ID passed isn't a member of the current array, don't want its matches:
if (!in_array($id, $group))
{
// Loop through each array as the ID isn't a member of this group:
foreach ($group as $member)
{
// Add the match the the matches array:
$matches[] = $member;
}
}
}
// Pass the matches back:
return $matches;
}
Finally looking for a single users matches:
// Find all the matches for ID 2 from the multidimensional array:
$matches = findCombos("2", $numberSet);
// Display the nubmer of matches:
echo "Found ".count($matches)." matches for 2.<br/>";
// Loop through each match found:
foreach ($matches as $match)
{
// Display the results:
echo "2 - ".$match."<br/>";
}
Results:
Found 8 matches for 2.
2 - 5
2 - 6
2 - 7
2 - 8
2 - 9
2 - 10
2 - 11
2 - 12
If you wanted to show all possibilities you could do something like this:
$count = 0;
foreach ($numberSet as $group)
{
foreach ($group as $member)
{
$matches = findCombos($member, $numberSet);
$count = $count+count($matches);
foreach ($matches as $match)
{
echo $member." - ".$match.", ";
}
}
}
echo "<br/>Found ".$count." possible combinations.";
Results:
1 - 5, 1 - 6, 1 - 7, 1 - 8, 1 - 9, 1 -
10, 1 - 11, 1 - 12, 2 - 5, 2 - 6, 2 -
7, 2 - 8, 2 - 9, 2 - 10, 2 - 11, 2 -
12, 3 - 5, 3 - 6, 3 - 7, 3 - 8, 3 - 9,
3 - 10, 3 - 11, 3 - 12, 4 - 5, 4 - 6,
4 - 7, 4 - 8, 4 - 9, 4 - 10, 4 - 11, 4
- 12, 5 - 1, 5 - 2, 5 - 3, 5 - 4, 5 - 9, 5 - 10, 5 - 11, 5 - 12, 6 - 1, 6 -
2, 6 - 3, 6 - 4, 6 - 9, 6 - 10, 6 -
11, 6 - 12, 7 - 1, 7 - 2, 7 - 3, 7 -
4, 7 - 9, 7 - 10, 7 - 11, 7 - 12, 8 -
1, 8 - 2, 8 - 3, 8 - 4, 8 - 9, 8 - 10,
8 - 11, 8 - 12, 9 - 1, 9 - 2, 9 - 3, 9
- 4, 9 - 5, 9 - 6, 9 - 7, 9 - 8, 10 - 1, 10 - 2, 10 - 3, 10 - 4, 10 - 5, 10
- 6, 10 - 7, 10 - 8, 11 - 1, 11 - 2, 11 - 3, 11 - 4, 11 - 5, 11 - 6, 11 -
7, 11 - 8, 12 - 1, 12 - 2, 12 - 3, 12
- 4, 12 - 5, 12 - 6, 12 - 7, 12 - 8,
Found 96 possible combinations.
If you chenage $numberSet to:
$numberSet = array( array("a","b"),
array("c", "d", "e", "f"),
array("joe", "tom", "same")
);
The result:
a - c, a - d, a - e, a - f, a - joe, a
- tom, a - same, b - c, b - d, b - e, b - f, b - joe, b - tom, b - same, c -
a, c - b, c - joe, c - tom, c - same,
d - a, d - b, d - joe, d - tom, d -
same, e - a, e - b, e - joe, e - tom,
e - same, f - a, f - b, f - joe, f -
tom, f - same, joe - a, joe - b, joe -
c, joe - d, joe - e, joe - f, tom - a,
tom - b, tom - c, tom - d, tom - e,
tom - f, same - a, same - b, same - c,
same - d, same - e, same - f,
You might want to take a look at array_diff(). I could see something like this working:
$everyone=range(1,12);
$groups=array(range(1,4), range(5,8), range(9,12));
$cnt=count($groups);
for($i=0;$i<$cnt;$i++) {
// this will give you all the people who aren't in your group
$diff=array_diff($everyone,$groups[$i]);
// loop and compare here
}
What wasn't clear to me is if the pair "1-5" and "5-1" are the same or not i.e. you need them to be unique pairs.
if it's only calculating pairs of 2 (and no higher), you can simply count the other two arrays.
for anyone in array1, simply count(array2) + count(array3) = number of pairs

Categories