How to group elements 5 by 5? - php

I have an array of about 40-something elements that I need grouped 5 by 5 like this:
<div class="five">
1
2
3
4
5
</div>
<div class="five">
6
7
8
9
10
</div>
What would be an elegant solution?

Just call array_chunk:
$numbers = range(1, 10);
$split = array_chunk($numbers, 5);

Try:
<?php
$i = 0;
foreach($array as $value){
if($i % 5 == 0){echo '<div class="five">';}
echo $value;
if($i % 5 == 4){echo '</div>';}
$i++;
}
?>

Related

Create line breaks in output based on array index

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;
}
}

How to use nested for() to get results intermittently and decreased?

I want this output:
1 1 1 1 1 1
2 2 2 3 3 3
4 4 5 5 6 6
7 8 9 10 11 12
I think I need to three nested For(), But I don't know how should I print the above result. Here is my code, How to complete it? (though I don't know, maybe my code is completely wrong)
for ($i=1; $i<=4; $i++) // row
{
for ($j=1; $j<=6; $j++) // column
{
for($z=1; $z<=12; $z++) // number
{
// what should be in here?
}
}
}
Edit: I want something like these examples: (Although these examples are very simple, what I want is a little more harder)
for ($i=1; $i<=4; $i++)
{
for ($j=1; $j<=6; $j++)
{
echo $i.' ';
}
echo '<br>';
}
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
Or this: echo $j;
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Edit2:
Note: I need to a code that be able to print this either: (its logic is the same with first output)
4 4 4 4 4 4
5 5 5 6 6 6
7 7 8 8 9 9
10 11 12 13 14 15
You can try something like this.
var $c = 1;
for ($i=1; $i<=4; $i++)
{
var $noOfChanges = 6/$i;
for ($j=1; $j<=6; $j++)
{
echo $c.' ';
if($j%$noOfChanges==0){
$c = $c + 1;
}
}
echo '<br>';
}
Not tested.
You can intialize the var $c = 4; to get the next pattern.
Tested and working:
$length = 6;
$row = 0;
$number = 1;
$total = 0;
$n = $length;
while(true) {
$n = floor($length/($row+1));
for($i = 0; $i<$n; $i++) {
echo $number;
echo "\t";
}
$total+=$n;
if($total >= $length) {
$row++;
$total = 0;
echo "\n";
if($n == 1 ) break;
}
$number++;
}

PHP: If number is divisble by 3, minus 1 (2, 5, 8, 11 etc)

I have a loop that creates divs with some content from a database. I have a variable $current_count which I'm starting at value '0' which is the first iteration of my loop.
I am using:
if ($current_count == 0 || $current_count % 3 == 0) { echo '<div class="parent">'; }
To create a parent div at the very top of the loop, then again on every iteration divisible by 3. It looks like this (with numbers representing iteration):
0 <div class="parent">
0 <div class="child"></div>
1 <div class="child"></div>
2 <div class="child"></div>
3 <div class="parent">
3 <div class="child"></div>
4 <div class="child"></div>
5 <div class="child"></div>
But the problem is I can't work out how to close those divs, as they would close on different iterations. For example, the parent opened on iteration 0 would need to be closed at the end of iteration 2.
I need to basically say (pseudo-code):
IF $current_count is equal to (division of 3, minus 1) { etc }
I have tried:
if ($current_count % 3 == (0 - 1)) {}
if ($current_count % (3 == 0) - 1) {}
if ($current_count % 3 == 0 - 1) {}
But none of these are returning true. Does anyone know a way I can do this?
Cheers,
Lee.
UPDATE 1: Here's an example of the PHP code currently, to better explain what I'm trying to accomplish:
$current_count = '0';
$ret = '';
foreach ( $brands as $index => $brand ) :
if ($current_count == 0 || $current_count % 3 == 0) {
$ret.= '<div class="parent">'; //Start parent
}
$ret.= '<div class="child"></div>'; //Child
if ($current_count % 3 == (0 - 1)) { // IF LINE 2, 5, 8, 11 etc, NOT WORKING
$ret.= '</div>'; // End the parent
}
$current_count++;
endforeach;
try this,
for($i = 0; $i <= 10; $i++) {
if($i % 3 == 0 && $i > 0)// $i > 0 condition because. 0 % 3 is equal to 0 only.
echo $i - 1;// will echo 2,5,8
echo "</div>";// in your case.
}
if you do it like that it still divided by 3 and not solving the question.
it should be :
if( $key % 3 == 2 ){
</div>
}

How to range and sequence in PHP

i need a sequence like this in a range from 0 to 10 with PHP. The result would be:
0
1
2
3
4
5
6
7
8
9
10
0
1
2
3
4
5
6
7
8
9
10
0
1
2
3
4
5
6
7
8
9
10
........
I want to insert this function inside a foreach
UPDATE
<?php $count = 0; ?>
<div class="<?php echo 'minipost-'. $count++ ; ?>">
<?php
get_template_part( 'content', get_post_format() );
?>
</div>
You already mentioned the solution in your question. You can use:
range(0, 10)
To repeat the sequence 3 times you could do:
str_repeat(implode(' ',range(0, 10)).' ', 3);
Without foreach;
$iCounter = 2;
$iCount = 10;
for ($k = 1; $k <= $iCounter; $k++) {
for ($i = 0; $i <= $iCount; $i++) {
// do anything you need
echo $i;
}
}

WordPress/PHP - How can I include a container div around every 9 images?

I have the following code:
<?php
$images = get_post_meta(get_the_ID(), 'pointb_portfolio_images', false);
$i = 0;
foreach ($images as $att) {
$i++;
$src = wp_get_attachment_image_src($att, 'full');
$src = $src[0];
echo "<img src='$src' class='t$i'/>";
}
?>
What I would like to do, is for every 9 images, place a DIV container around them. So it would be:
<div>9 images here</div>
<div>next 9 images here</div>
<div>next 9 images here</div>
I have an incremental class being applied to each image, and this would need to continue increasing upward.
I have been googling to try and find a solution for this, but I am struggling to find even the correct search query.
Would appreciate any assistance or tips so that I can accomplish what I need.
Thanks
You can use % (modulus) it finds the remainder. So what you do is you have an if ($i % 9 == 0) ... then close the 9 image div and open a new one. That expression will validate as true once every 9 loops through. You also have an opening <div> before the loop starts and a closing </div> after it's done.
<?php
$images = get_post_meta(get_the_ID(), 'pointb_portfolio_images', false);
$i = 0;
echo '<div>';
foreach ($images as $att) {
// Moved this to the front of the loop so we don't have any empty div groups
// in case 9 is a factor of the number
if ($i > 0 && $i % 9 == 0) {
echo '</div><div>';
}
$src = wp_get_attachment_image_src($att, 'full');
$src = $src[0];
// Added modulus to the class so now they will be t1, t2,... t8, t9, t1, t2...
echo "<img src='$src' class='t" . ($i % 9 + 1) . "'/>";
$i++;
}
echo '</div>';
?>
Examples of what Modulus returns
0 % 9 = 0
1 % 9 = 1
2 % 9 = 2
3 % 9 = 3
4 % 9 = 4
5 % 9 = 5
6 % 9 = 6
7 % 9 = 7
8 % 9 = 8
9 % 9 = 0
10 % 9 = 1
11 % 9 = 2

Categories