0

I want to run specific command 4 times in a loop. How do i do that?

$list = array(
"data1", "data2", "data3", "data4", "data5", "data6", "data7", "data8", "data9", "data10", "data11", "data12", "data13", "data14", "data15", "data17"
);

$split_by = 4;
$total_execute = round(count($list)/$split_by);
for($i=0; $i<=count($list); $i++)
{
//1
//2
//3
//4 //execute command 1
//5
//6
//7
//8 //execute command 2
//9
//10
..............

}

How can i execute command in every 4 index and total $total_execute time in php? I have tried but its not working. i have no idea.

3
  • okay edited. could you answer? I mean how do i execute $total_execute execute times only? Commented Dec 8, 2018 at 11:04
  • Look at the modulus operator. Commented Dec 8, 2018 at 11:06
  • What is $total_execute needed for? Commented Dec 8, 2018 at 11:12

2 Answers 2

1

If you only want to process every fourth item in the list, you can construct the loop to do that. (NB the fourth item is index 3).

for ($i=3; $i<count($list);$i+=4) {
//command
}
Sign up to request clarification or add additional context in comments.

Comments

0

The for-loop should be:

for($i = $split_by - 1; $i < count($list); $i += $split_by)
{
    for ($j = 0; $j < $total_execute; j++) {
        $elem = $list[$i];
        // Do something with '$elem'...
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.