I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)
Related
I need your help to solve this problem, I am trying to write the necessary code so that the output is as follows:
PHP! PHP!! PHP!!! PHP!!!! PHP!!!!!
<?php
$y = ['!'];
for($i = 1; $i <= 5; $i++)
{
print "PHP!"."$y\t";
if($y+1 <5)
$y="!";
$y+'!';
}
?>
Until the second word, it adds one more exclamation point and in the rest of the repetitions it has the same number, that is two exclamation points.
How can I solve this problem?
Try this:
<?php
for ($i = 0; $i < 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
You can try it:
<?php
for($i = 1; $i <= 5; $i++)
{
echo "PHP";
for($j = 1; $j <= $i; $j++){
echo "!";
}
}
?>
You can also use str_repeat():
<?php
for ($i = 1; $i <= 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
Output will be: PHP!PHP!!PHP!!!PHP!!!!PHP!!!!!
Can anyone help me understand why a variable takes its initial value after incrementing the variable? below is code:
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
echo $k+3;
echo $l+3;
}
In this we have two for loops running one inside other. Here we run three times the outside for loop, inside this we are running other for loop again. The problem we are facing is that when inner for loop end we have incremented $k and $l both by 3 but it always take value 0 and 3 respectively.
we have incremented $k and $l both by 3
Nope, you only print the result of your values plus 3, but you do not set them anywhere in the loop:
Instead of
echo $k+3;
echo $l+3;
write
$k = $k + 3;
$l = $l + 3;
You should try removing the "echo" and incrementing the variable in each loop. Then print them out after.
Try this:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
$j = $j++;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
?>
Gives you:
9
12
TRY this.
$k += 3;
$l += 3;
echo $k;
echo $l;
Try This
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
First Increment the value and store it in the variable
$k = $k+3;
$l = $l+3;
Then You need to print using
echo $k.'<br>';
echo $l;
#Harinarayan First of all you need to read about echo() http://php.net/manual/en/function.echo.php
echo — Output one or more strings
echo does not manipulate expression as you did in your question like:
echo $k+3;
instead of using echo for the increment you should first increment the variable and then echo that variable like below:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k += 3;
$l += 3;
echo $k;
echo "<br>";
echo $l;
}
How would I group 5 numbers in an array into each line? I've tried this code below but it results in something I'm not expecting it to be.
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
for ($i=0; $i<5; $i++)
{
echo ($result_data[$i]);
}
echo ("\n");
}
Result:
239298246244268
239298246244268
239298246244268
239298246244268
This loop keep repeating the first 5 numbers in my array. How do I make it to loop for every 5 numbers instead in my whole array of numbers? Thank you!
$x should be your index for the echo. Try this instead:
<?php
$result_data = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
for ($x = 0; $x < count($result_data); $x++)
{
echo ($result_data[$x]);
if(($x+1)%5==0)
{
echo ("\n");
}
}
use this $result_data[$x]
try this
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
if($x%5==0)
{
echo ("\n");
}
echo ($result_data[$x]);
}
Don't know much about your $result_data Array, but probably it should go like this:
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
for ($i=0; $i<5; $i++)
{
echo ($result_data[$x][$i]);
}
echo ("\n");
}
I think you want to do something like this
$i = 0;
foreach($result_data as $result) {
echo $result;
if($i < 5) {
echo ",";
} else {
echo "<br/>\n";
$i = 0;
}
$i++;
}
Something like this?
$chunks = array_chunk($result_data, 5);
foreach($chunks as $chunk) {
echo implode('', $chunk);
echo "\n";
}
See http://uk3.php.net/manual/en/function.array-chunk.php
Try this several line code:
$valuesDelimiter = ', ';
$lineDelimiter = "\n";
$input_array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$slited_array = array_chunk($input_array, 5);
array_walk($slited_array, function(&$arr) {$arr = implode($valuesDelimiter, $arr);});
$result = implode($lineDelimiter, $slited_array);
So, what à do is trying to display the 3 first element of an array. Always 3. But, there is not always at least 3 elements.
So, what I've done is using an if :
<?php for ($i = 0; $i <= 2; $i++) { ?>
<?php if($post["Project"]["Post"][$i]){ ?>
...
<?php } ?>
<?php } ?>
But, I keep having "undefined offset" error when there is not 3 entries at least. Anyone with a solution ?
foreach (array_slice($array, 0, 3) as $item) {
echo $item;
...
}
or:
$i = 1;
foreach ($array as $item) {
...
if ($i++ >= 3) {
break;
}
}
foreach is always preferable to iterate arrays, precisely because you cannot access anything that doesn't exist.
Try the following:
<?php for ($i = 0; $i <= 2; $i++) {
if(isset($post["Project"]["Post"][$i])){ ?>
...
<?php }} ?>
Use isset() to check whether the key exists.
<?php if(isset($post["Project"]["Post"][$i])){ ?>
Or you could use:
$posts = $post["Project"]["Post"];
foreach ($posts as $i => $post) {
//...
if ($i === 2) break;
}
change it to if (isset($post ...
Alternatively:
foreach (array_slice($post["Project"]["Post"], 0, 3)) { ...
<?php for ($i = 0; $i <= 2; $i++) {
if( isset($post["Project"]["Post"][$i] )){
...
}
} ?>
you do not need <?php...?> tags for every line.
try ifset.
<?php for ($i = 0; $i <= 2; $i++) { ?>
<?php if(isset($post["Project"]["Post"][$i])){ ?>
...
<?php } ?>
<?php } ?>
Or, do a count first could be a way too.
Calculate before, which condition is met first (the actual size of the array or the max number - in your case 3) and then just traverse those elements:
$min = min( 3, count( $post["Project"]["Post"] ) );
for ($i = 0; $i < $min; $i++) {
if($post["Project"]["Post"][$i]){
...
}
}
For example in a for loop you can kick out like this:
for($i = 0; $i < count($ary); $i++){
if($ary[$i] == 'blah')
$i = count($ary);
echo $i;
}
Or in a while loop:
$i = 0;
while($i < count($ary)){
if($ary[$i++] == 'blah')
$i = count($ary);
echo $i;
}
Not sure really what you mean by "kick out", but:
To skip to the next item, use continue;
To stop the entire loop, use break;
If I understand your question correctly what you are looking for is the break keyword.
PHP Break
Use break
for($i = 0; $i < count($ary); $i++){
if($ary[$i] == 'blah')
break;
echo $i;
}
for($i = 0; $i < count($ary); $i++){
if($ary[$i] == 'blah')
break;
echo $i;
}
foreach($ary as $c){
if($c=='blah')break;
}
Manual