- First we set a variable $count to 0 [Integer Type]
- Now on each loop of the foreach we increase the value of $count by 1.
- Just after the increment, inside the foreach loop we check if the value of $count is equal to the desired value of looping. If the value is equal then we just break the loop using break;
Revisions
- April 19, 2013 @ 16:07:41 [Current Revision] by PeterLugg
- April 19, 2013 @ 16:06:54 by PeterLugg
Revision Differences
April 19, 2013 @ 16:06:54 | Current Revision | ||
---|---|---|---|
Content | |||
Added: Basically we are going to use the following algorithm: | |||
Deleted: | Added: <ul> | ||
Added: <li>First we set a variable <strong> $count </strong>to 0 [<em>Integer Type</em>]</li> | |||
Added: <li>Now on each loop of the <strong> foreach </strong>we increase the value of <strong>$count< /strong> by 1.</li> | |||
Added: <li>Just after the increment, inside the <strong> foreach</strong> loop we check if the value of <strong>$count< /strong> is equal to the desired value of looping. If the value is equal then we just break the loop using <strong> break;</strong></li> | |||
Added: </ul> | |||
Added: Here is the code: | |||
Added: [php] | |||
Added: <pre>$max_loop=5; //This is the desired value of Looping</pre> | |||
Added: &nbsp; | |||
Added: <pre>$count = 0; //First we set the count to be zeo</pre> | |||
Added: &nbsp; | |||
Added: <pre>echo "<h2> Here goes the values</h2> ";</pre> | |||
Added: &nbsp; | |||
Added: <pre>foreach($my_array as $key => $val) {</pre> | |||
Added: &nbsp; | |||
Added: <pre> echo "The value of $key is $val<br/>"; //Print the value of the Array</pre> | |||
Added: &nbsp; | |||
Added: <pre> $count++; //Increase the value of the count by 1</pre> | |||
Added: &nbsp; | |||
Added: <pre> if($count==$max_loop) break; //Break the loop is count is equal to the max_loop</pre> | |||
Added: &nbsp; | |||
Added: <pre>}</pre> | |||
Added: [/php] |
Note: Spaces may be added to comparison text to allow better line wrapping.
No comments yet.