This question already has answers here:
Modulus in a PHP loop
(7 answers)
Closed 8 months ago.
can u please help me on this. I have an array with 6 items, I want to print 2 items per line separated by commas, I have this code:
$rates[] = array("2020-01-01"=>3000);
$rates[] = array("2020-01-02"=>3010);
$rates[] = array("2020-01-03"=>3020);
$rates[] = array("2020-01-04"=>3021);
$rates[] = array("2020-01-05"=>3030);
$rates[] = array("2020-01-06"=>3035);
$rates_count = count($rates);
$total = $rates_count;
$col = 2;
for ($i = 0; $i < $total; $i++) {
$date = key($rates[$i]);
$value = end($rates[$i]);
if ($i % $col != 0) {
echo "{$date},";
echo "{$value},";
} else {
echo "{$date},";
echo "{$value}";
echo "</br>";
}
}
This code print this:
2020-01-01,3000
2020-01-02,3010,2020-01-03,3020
2020-01-04,3021,2020-01-05,3030
2020-01-06,3035,
I want this:
2020-01-01,3000,2020-01-02,3010
2020-01-03,3020,2020-01-04,3021
2020-01-05,3030,2020-01-06,3035
Where is the error? Thanks for any help.
I tested this and your output should be correct now
if ($i % $col == 0) {
echo "{$date},";
echo "{$value},";
} else {
echo "{$date},";
echo "{$value}";
echo "</br>";
}
Output:
2020-01-01,3000,2020-01-02,3010
2020-01-03,3020,2020-01-04,3021
2020-01-05,3030,2020-01-06,3035
I changed the comparison to be " == 0"
The first value in the iteration will be cero because 0 % 2 = 0
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0
5 % 2 = 1
... and so on
The condition was returing false in the first iteration, it was fixed by changing the comparison of the remainder to be equal to cero.
Related
I have a cloumn like token_number in table the values inserted in this row are like
token_number
1
2
3
15
4
5
6
14
7
8
18
10
11
9
etc..
I sort these values
1,2,3,4,5,6,7,8,9,10,11,14,15,18
I want to get the value 11 from this sorted array
ie the highest number with common difference 1
How to get 11 from this array?
Since the array is sorted, you can start from the bottom e loop it backwards:
$arr = // sorted array;
$max = null;
for( $i = count($arr) - 1 ; $i > 0 ; $i++ ){
if($arr[$i] - 1 == $arr[$i-1]){
$max = $arr[$i-1];
break;
}
}
if($max !== null){
echo "founded: " . $max;
}
You can find highest number with command difference as follow.
$arr= array(1,2,3,4,5,6,7,8,9,10,11,14,15,18);
$highestNo = null;
for($i=0 ;$i <= count($arr) - 1 ; $i++ ){
$diff = $arr[$i+1] - $arr[$i]; //check difference of current and next number
if($diff != 1){
$highestNo = $arr[$i];
break;
}
}
echo "Highest number with comman difference is: " . $highestNo;
can you guys please help me for this? I want to loop numbers from 1 to 100..
if the number is multiple of 4 it will print 'NEW', if number is multiple of 7 it will print 'TEST', and if the number is multiple of both 4 and 7 it will print 'NEWTEST'.
I've output the multiple of 4 and multiple of 7 but in both 4 and 7 I cant print the 'NEWTEST'.
Here's my code.
Thank you guys
function primeno($n){
for($i = 1; $i < 100; $i++){
if ($i % 4 == 0){
echo 'easy<br>';
}else if($i % 7 == 0){
echo 'EMPLOYER<br>';
}
else if($i % 4 == 0 && $i % 7 == 0){
echo 'easyEMPLOYER<br>';
}else{
echo $i."<br>";
}
}
}
primeno(100);
Here's my output:
1
2
3
NEW
5
6
TEST
NEW
9
10
11
NEW
13
TEST
15
NEW
17
18
19
NEW
TEST
22
23
NEW
25
26
27
NEW ----> it should be NEWTEST
29
30
No. 28 should be output 'NEWTEST' but instead it output NEW
You just need to swap your conditions in your "if" and your last "else if" statements
<?php
function primeno($n) {
for ($i = 1; $i < 100; $i++) {
if ($i % 4 == 0 && $i % 7 == 0) {
echo 'easyEMPLOYER<br>';
}
else if ($i % 7 == 0) {
echo 'EMPLOYER<br>';
}
else if ($i % 4 == 0) {
echo 'easy<br>';
}
else {
echo $i . "<br>";
}
}
}
primeno(100);
?>
I want to display output like this:
1 2 3 4
5 6 7 8
9 1 2
I've tried this code:
$num = ['1','2','3','4','....'];
$size = sizeof($num) / 4;
foreach ($num as $key => $value) {
echo $value;
if($key >= round($size){
echo "<br>"
}
}
But the output is like this:
1 2 3 4
5
6
7
8
...
Can anyone suggest how to write the loop?
$num= ['1','2','3','4','5','6','7','8','9'];
$size = sizeof($num) / 4;
foreach ($num as $key => $value){
echo $value;
if(($key+1) % 4 == 0){
echo "<br>";
}
}
You can use modulus instead of round. Cool I didn't know about sizeOf! Good to know. Mark this as the right answer pwease if this works!
Another way to do this if you didn't want to write out all the numbers that are in the Num Array is to just push them into an array with a while loop.
$num= [];
$i = 1;
//Set the Num Variable to have as many numbers as you want without having to manually enter them in
while ($i < 100) {
array_push($num, $i);
$i++;
}
//Run the actual code that adds breaks ever 4 lines
$size = sizeof($num) / 4;
foreach ($num as $key => $value){
echo $value;
if(($key+1) % 4 == 0){
echo "<br>";
}
}
Sorry if this answer looks the same as the first answer but I will explain it clearer
To achieve what you want
Step 1: Create a for loop
The loop will start from 1 to it's total size of the array
for ($x = 1; $x <= sizeof($num); $x++){
}
Then inside your loop
you can use ternary for simplicity
This line of code
# if $x variable is equal to limit number which you wanted to break
# $num[$x-1] -> subtract to by 1 because we know array always start at index 0
if ($x % 4 == 0) {
$num[$x-1]."<br>"; #put a break after it
} else {
echo $num[$x-1];
}
is same as this
echo ($x % 4 == 0) ? $num[$x-1]."<br>" : $num[$x-1];
So try this
<?php
$num= ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'];
$size = sizeof($num) / 4;
for ($x = 1; $x <= sizeof($num); $x++){
echo ($x % 4 == 0) ? $num[$x-1]."<br>" : $num[$x-1];
}
DEMO
You can try this:
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 8, 19, 20];
$len = 1;
foreach ($numbers as $number) {
echo $number . ' ';
$len++;
if ($len > 4) {
echo '<br>';
$len = 1;
}
}
What i want to print is
1
3 5
7 9 11
With my current code , that is ...
<?php
function Odd($limit='20'){
$c = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c ;
echo "<br/>";
}
$c++ ;
}
}
Print Odd();
?>
i am getting
1
3
5
7
9
11
Can someone please guide me the right way ?
Aaah ... ok.^^ Now i got it.
Its pretty easy: You need another variable which counts up and one which limits the breakposition. Looks like this:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
}
$c++ ;
}
}
Print Odd();
?>
Output till 40:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
31 33 35 37 39
Edit: Code for your new request:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
$c++ ;
}
}
Print Odd();
?>
So if I understand correctly you want to output something like that:
1
3 5
7 9 11
13 15 17 19
Here is my solution:
function Odd($limit='20'){
$c = 1;$some_array = array();
while($c <= $limit){
if ($c % 2!=0){
$some_array[]=$c;
}
$c++ ;
}
return $some_array;
}
$array = Odd();
$nr =0;
$j=1;
foreach ($array as $key => $value) {
echo $value.' ';$nr++;
if($nr==$j){
echo '<br />';
$nr=0;
$j++;
}
}
Hope this helps!
From your question it Seems you are really new to programming so before writing any program first of all observe the question properly:
For example for the question above it is clear that is an triangle of odd numbers.
now the number of odd numbers on each row is equal to the row
i.e 1st row contains 1 number ,2nd contains 2 and it continues...
Now what we do is take an variable to count the no of rows say $row and the other will be $limit .
<?php
function odd($limit){
$row=1;
$current_number=1;
while($current_number<=$limit){
for($i=1;$i<=$row;$i++){
echo $current_number." ";
$current_number=$current_number+2;//incrementing numbers by 2 if you want to increment by 1 i.e print all numbers replace 2 by 1
}
$row++;
echo "<br/>";//for new line
}
}
To run above function you need to call it and pass the value of $limit.To do it just type anywhere outside of this function.
odd(20);
Watch this running here:
I need this output..
1 3 5
2 4 6
I want to use array function like array(1,2,3,4,5,6). If I edit this array like array(1,2,3), it means the output need to show like
1 2 3
The concept is maximum 3 column only. If we give array(1,2,3,4,5), it means the output should be
1 3 5
2 4
Suppose we will give array(1,2,3,4,5,6,7,8,9), then it means output is
1 4 7
2 5 8
3 6 9
that is, maximum 3 column only. Depends upon the the given input, the rows will be created with 3 columns.
Is this possible with PHP? I am doing small Research & Development in array functions. I think this is possible. Will you help me?
For more info:
* input: array(1,2,3,4,5,6,7,8,9,10,11,12,13,14)
* output:
1 6 11
2 7 12
3 8 13
4 9 14
5 10
You can do a loop that will automatically insert a new line on each three elements:
$values = array(1,1,1,1,1);
foreach($values as $i => $value) {
printf('%-4d', $value);
if($i % 3 === 2) echo "\n";
}
EDIT: Since you added more information, here's what you want:
$values = array(1,2,3,4,5);
for($line = 0; $line < 2; $line++) {
if($line !== 0) echo "\n";
for($i = $line; $i < count($values); $i+=2) {
printf('%-4d', $values[$i]);
}
}
And if you want to bundle all that in a function:
function print_values_table($array, $lines = 3, $format = "%-4d") {
$values = array_values($array);
$count = count($values);
for($line = 0; $line < $lines; $line++) {
if($line !== 0) echo "\n";
for($i = $line; $i < $count; $i += $lines) {
printf($format, $values[$i]);
}
}
}
EDIT 2: Here is a modified version which will limit the numbers of columns to 3.
function print_values_table($array, $maxCols = 3, $format = "%-4d") {
$values = array_values($array);
$count = count($values);
$lines = ceil($count / $maxCols);
for($line = 0; $line < $lines; $line++) {
if($line !== 0) echo "\n";
for($i = $line; $i < $count; $i += $lines) {
printf($format, $values[$i]);
}
}
}
So, the following:
$values = range(1,25);
print_array_table($values);
Will output this:
1 10 19
2 11 20
3 12 21
4 13 22
5 14 23
6 15 24
7 16 25
8 17
9 18
One solution is to cut the array into chunks, representing the columns, and then print the values in row order:
$cols = array_chunk($arr, ceil(count($arr)/3));
for ($i=0, $n=count($cols[0]); $i<$n; $i++) {
echo $cols[0][$i];
if (isset($cols[1][$i])) echo $cols[1][$i];
if (isset($cols[2][$i])) echo $cols[2][$i];
}
If you don’t want to split your array, you can also do it directly:
for ($c=0, $n=count($arr), $m=ceil($n/3); $c<$m; $c++) {
echo $arr[$c];
for ($r=$m; $r<$n; $r+=$m) {
echo $arr[$c+$r];
}
}
$a = array(1,2,3,4,5);
"{$a[0]} {$a[1]} {$a[2]}\n{$a[3]} {$a[4]}";
or
$a = array(1,2,3,4,5);
"{$a[0]} {$a[1]} {$a[2]}".PHP_EOL."{$a[3]} {$a[4]}";
or
$a = array(1,2,3,4,5);
$second_row_start = 3; // change to vary length of rows
foreach( $a as $index => $value) {
if($index == $second_row_start) echo PHP_EOL;
echo "$value ";
}
or, perhaps if you want a longer array split into columns of 3?
$a = array(1,2,3,4,5,6,7,8,9,10,11,12,13);
$row_length = 3; // change to vary length of rows
foreach( $a as $index => $value) {
if($index%$row_length == 0) echo PHP_EOL;
echo "$value ";
}
which gives
1 2 3
4 5 6
7 8 9
10 11 12
13
one solution is :
your array has N elements, and you want 3 columns, so you can get the value of each cell with $myarray[ column_index + (N/3) + line_index ] (with one or two loops for columns and lines, at least for lines)
I hope this will help you
Bye
Here's something I whipped up. I'm pretty sure this could be more easily accomplished if you were using HTML lists, I've assumed you can't use them.
$arr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14, 15, 16);
$max = count($arr);
$cols = 3;
$block = ceil($max / $cols);
for ($i = 0; $i < $block ; $i++) {
echo $arr[$i] . ' ';
for ($j = 1; $j < $cols; $j++) {
$nexKey = $i + $block * $j;
if (!isset($arr[$nexKey])) break;
echo $arr[$nexKey] . ' ';
}
echo '<br>';
}
NOTE : You can easily refactor the code inside the loop that uses $nexkey variable by making it into a loop itself so that it works for any number of columns. I've hardcoded it.
Uses loops now.