1

I'm trying to count elements in a loop to break each number of elements and show in groups

My little script

$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
    print "".$exp_filter[$x]."";
    if ($x%5 == 0)
    {
        print "<br>";   
    }

}

As you can see in the little script each 5 rounds I want show the tag
for break and show as in groups of elements.

The problem it´s always show in the first line one element and after this the rest, and no works fine.

1
  • 2
    $x%5 maybe returns 0 if $x is 0. Try this: if($x > 0 && $x%5==0)
    – t.h3ads
    Nov 19, 2014 at 10:54

4 Answers 4

1

The index of $exp_filter starts at 0, so this block of code

if ($x % 5 == 0)
{
    print "<br>";   
}

should be

if (($x+1) % 5 == 0)
{
    print "<br>";   
}

Here's the complete modified code

$data = "house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
    print "".$exp_filter[$x]."";
    if (($x + 1) % 5 == 0)
    {
        print "<br>";   
    }
}

Working example: http://codepad.org/iEsKK98M

3
  • But with the last element won't throw an out of bound index array?
    – Marco Mura
    Nov 19, 2014 at 11:01
  • No, since there's no $exp_filter[$x+1] anywhere inside the for loop.
    – ekad
    Nov 19, 2014 at 11:02
  • @MarcoMura Nope, the value is not used as an array key
    – Steve
    Nov 19, 2014 at 11:02
1
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
  print "".$exp_filter[$x]."";
  if($x%5==0) 
  {
    print "<br>";   
  }
}

Try this. The problem is you started at 0 in for, you should start from 1 ;)

2
  • No work me , the first element no show , i try also this and the problem it´s the first elemt in the array with [0] no show if put 1 and no 0 and if put 0 also have problems :(
    – Francisco
    Nov 19, 2014 at 10:59
  • so where do you want the first element to be? do you want it to be 6 elements in the first line then 5 or 5 elements and the last one sigle? How? Nov 19, 2014 at 11:00
1

Quickfix:

Demo

<?php

$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x > 0 && $x%5==0) 
{
echo "<br />";   
}

}
?>
1

I would use array chunk and implode instead:

$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

foreach(array_chunk(explode('*', $data), 5) as $chunk){
    echo implode(' ', $chunk) . '<br>';
}

Live example: http://codepad.viper-7.com/ED2wHR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.