How to right align numbers from an array in php? - 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

Related

How can I print this sequence of numbers using PHP?

I want to try this sequence in PHP: 10 9 8 7 6 5 4 3 2 1 3 4 5 6 7 8 9 10. The condition is I can use only one loop.
I tried this code and I know it will infinite after it prints 0. But I do not find any way to fix it.
<?php
for($i = 10; $i >= 0; $i--){
echo $i;
if($i == 0){
echo $i++;
}
}
Now I can't test it but I think it works:
<?php
$x = 10;
$cond = "DESC";
for($i = 20; $i >= 0; $i--){
echo $x;
If ( $x == 1 ) $cond = "ASC";
$cond == "DESC" ? $x-- : $x++;
}
?>
If you want to have a more simple solution you can use array:
<?php
$arr = Array (10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10);
foreach ($arr as $int){
echo $int;
}
?>
You could do this with a one-liner, if you're looking for "simple":
foreach (array_merge(range(10, 1, 1), range(3, 10, 1)) as $i) {
echo "$i ";
}
Result:
10 9 8 7 6 5 4 3 2 1 3 4 5 6 7 8 9 10
This makes the two arrays, [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ] and [ 3, 4, 5, 6, 7, 8, 9, 10 ], concatenates them, and then runs through the resultant array.

PHP Gradual calculate

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

Nested for and do while loop

How do i create a nested loop that will output the following numbers?
each round the inner loop increase from 1 to 5, 5 to 10 and so on.
from i = 0 to 5
inner loop:
result: 1 2 3 4 5
result: 6 7 8 9 1 0
result: 11 12 13 14 15
result: 16 17 18 19 20
next
for($i=0;$i<50;$i++)
{
$s = $i +5;
echo $s;
}
unless you have to use an inner do...while loop, this will work:
<?php
$max = 5;
for($i=0;$i<$max;$i++){
for($j=1;$j<=$max;$j++){
echo str_pad(($i*$max)+$j,4);
}
echo "\r\n";
}
output:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
I used str_pad() just to make the columns more uniform

PHP :1D array to 2D array

Hi I have a 1D array (1 by 20) that I would like to transform to a 2D Array (4 by 5)
$winning_number = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
to
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
right now I am using this code:
foreach ($wining_no as $boulex)
{
for($i=0;$i<$5;$i++)
{
if($i==0)
{
for($j=0;$j<$4;$j++)
{
$boule_array[$j][$i] = $boulex;
}
}
}
}
For some reason this does not work
You could use the array_chunk($array, $size) function
For you it would be like this
array_chunk($winning_number, 5);

How does this code generate the proper sequence of characters?

The code in question comes from MathGuard, a PHP anti-spam CAPTCHA script that requires the user to answer a simple math problem. It displays the digits and operator symbols as 3x5 matrices of random characters. I understand how the code works in the sense that I can follow the code and understand what it's doing; I just don't understand how one would come to this solution.
This function takes an integer that describes one line of the 3x5 matrix and converts it into a line of random characters:
function decToBin($dec) {
$pattern = "123456789ABCDEFGHIJKLMNOPQRTSTUWXYZ";
$output = " ";
$i = 0;
do {
if ($dec % 2) {
$rand = rand() % 34;
$output { 2 - $i } = $pattern { $rand };
} else {
$output { 2 - $i } = " ";
}
$dec = (int) ($dec / 2);
$i++;
} while ($dec > 0);
$output = str_replace(" ", " ", $output);
return $output;
}
Here are the digit descriptors:
$number = array (
array ( 7, 5, 5, 5, 7 ), // 0
array ( 2, 6, 2, 2, 7 ), // 1
array ( 7, 1, 7, 4, 7 ), // 2
array ( 7, 1, 7, 1, 7 ), // 3
array ( 4, 5, 7, 1, 1 ), // 4
array ( 7, 4, 7, 1, 7 ), // 5
array ( 7, 4, 7, 5, 7 ), // 6
array ( 7, 1, 1, 1, 1 ), // 7
array ( 7, 5, 7, 5, 7 ), // 8
array ( 7, 5, 7, 1, 7 ) // 9
);
My question is: how does one come to this conclusion and method of generation and know that, for example, 7 will generate a full line of random characters, and 5 only the outermost characters?
Is this just a form of code obfuscation? What makes this method better than, say, storing the digits as a string (111101101101111 as 0, for example) and replacing each 1 with a random character?
Looks like a simple bitmap to me.
7 = 1 1 1
5 = 1 0 1
5 = 1 0 1
5 = 1 0 1
7 = 1 1 1
2 = 0 1 0
6 = 1 1 0
2 = 0 1 0
2 = 0 1 0
7 = 1 1 1

Categories