How to perform an action every 5 results?

前端 未结 6 1489
春和景丽
春和景丽 2020-11-30 09:28

How can I perform an action within a for loop every 5 results?

Basically I\'m just trying to emulate a table with 5 columns.

6条回答
  •  星月不相逢
    2020-11-30 09:54

    This works to get a live index within the foreach loop:

     'bar', 'go' => 'habs', 'CSGO_bestTeam' => 'fnatic', 'test' => 'one two', 'potato' => 'french fries', 'tomato' => 'ketchup', 'coffee' => 'expresso', 'window' => 'cleaner', 'truck' => 'load', 'nine' => 'neuf', 'ten' => 'dix');
    
    // Numeric-Index Array of the Named-Index Array
    $myNumIndex = array_keys($myNamedIndexArray);
    
    
    foreach($myNamedIndexArray as $key => $value) {
        $index = array_search($key,$myNumIndex);
    
        if ($index !== false) {
            echo 'Index of key "'.$key.'" is : '.$index.PHP_EOL;
    
            if (($index+1) % 5 == 0) {
                echo '[index='.$index.'] stuff to be done every 5 iterations'.PHP_EOL;
            }
        }
    
    }
    

提交回复
热议问题