t_machine Posted October 29, 2007 Share Posted October 29, 2007 Hi, I am wondering if someone could help me with this problem. I have an array that contains both key and value. The first key/value is used to store a heading so it is not needed in the rest of the loop. How can I skip that first key/value and display the rest. Example: $myarray = array("key1"=>"heading", "key2"=>1, "key3"=>2....... Without knowing the key/value of the first result, how can I skip it foreach($myarray as $k =>$v){ //if first key skip //else continue the loop } Thanks in advance Link to comment https://forums.phpfreaks.com/topic/75265-solved-how-to-skip-first-result-in-array-foreach-loop/ Share on other sites More sharing options...
Wuhtzu Posted October 29, 2007 Share Posted October 29, 2007 A real simple use would be to add a counter: <?php $i = 1; foreach($myarray as $k =>$v){ if($i <> 1) { //Do stuff to all but the first record } $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/75265-solved-how-to-skip-first-result-in-array-foreach-loop/#findComment-380681 Share on other sites More sharing options...
Dragen Posted October 29, 2007 Share Posted October 29, 2007 You could try something like this: <?php $myarray = array("key1"=>"heading", "key2"=>1, "key3"=>2); $head = array_shift($myarray); foreach($myarray as $k =>$v){ //do what you like } ?> That'll remove the first key and value and save it as $head. Then you can go through the array without worrying about it. Afterwards you can use array_unshift($myarray, $head); to add it back to the beginning EDIT: wuhtzu's idea might be simpler, although you'd need to change this bit: if($i <> 1) { to this: if($i > 1) { Link to comment https://forums.phpfreaks.com/topic/75265-solved-how-to-skip-first-result-in-array-foreach-loop/#findComment-380682 Share on other sites More sharing options...
Orio Posted October 29, 2007 Share Posted October 29, 2007 You could do something like this... Not very memory efficient tho: <?php //$array is your array $temp = $array; array_shift($temp); foreach($temp as $k=>$v) //.... ?> Alternatively you could use an if: <?php //$array is your array $start = TRUE; foreach($array as $k=>$v) { if($start) $start = FALSE; else //.... } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/75265-solved-how-to-skip-first-result-in-array-foreach-loop/#findComment-380684 Share on other sites More sharing options...
t_machine Posted October 29, 2007 Author Share Posted October 29, 2007 Thank you all very much I figured there was a function to skip it but I wasn't sure which it was. Thanks again. Link to comment https://forums.phpfreaks.com/topic/75265-solved-how-to-skip-first-result-in-array-foreach-loop/#findComment-380687 Share on other sites More sharing options...
Wuhtzu Posted October 29, 2007 Share Posted October 29, 2007 Please let the rest of us know what you finally did... Link to comment https://forums.phpfreaks.com/topic/75265-solved-how-to-skip-first-result-in-array-foreach-loop/#findComment-380691 Share on other sites More sharing options...
t_machine Posted October 29, 2007 Author Share Posted October 29, 2007 Sorry about that, I went with the first reply using the $i counting. Thanks again Link to comment https://forums.phpfreaks.com/topic/75265-solved-how-to-skip-first-result-in-array-foreach-loop/#findComment-380703 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.