cgm225 Posted June 22, 2008 Share Posted June 22, 2008 I have a two dimensional array I create like so: $this->data[$id] = array( 'date' => $date, 'note' => $note, ); I then echo that array in a foreach loop. My question is, once that array is filled with a bunch of keys and values, how can I echo in a foreach loop only a certain range of those keys/values. Restated, if I have 100 pairs of keys and values, how could I only echo in the foreach loop those values between and including positions 85 to 95 in the array (this is even when the $id key is not a number value). Thanks in advance! Let me know if you need any clarification. Link to comment https://forums.phpfreaks.com/topic/111399-echoing-only-range-of-keyvalue-pairs-from-array-in-foreach-loop/ Share on other sites More sharing options...
trq Posted June 22, 2008 Share Posted June 22, 2008 foreach(range(85,95) as $pos) { echo $this->data[$pos]['date'] . " " . $this->data[$pos]['note']; } Link to comment https://forums.phpfreaks.com/topic/111399-echoing-only-range-of-keyvalue-pairs-from-array-in-foreach-loop/#findComment-571818 Share on other sites More sharing options...
cgm225 Posted June 22, 2008 Author Share Posted June 22, 2008 yes, but what if the $id key is not numerical in value? What if I have an array with various key values, like... apple => whatever 1 => house 4 => testing frog => green ...but I want to echo, in the foreach, the range of key/value pairs in the 2nd through, and including, 4th spots? Link to comment https://forums.phpfreaks.com/topic/111399-echoing-only-range-of-keyvalue-pairs-from-array-in-foreach-loop/#findComment-571824 Share on other sites More sharing options...
trq Posted June 22, 2008 Share Posted June 22, 2008 Associative array are not indexed by location so your question isn't really valid. Link to comment https://forums.phpfreaks.com/topic/111399-echoing-only-range-of-keyvalue-pairs-from-array-in-foreach-loop/#findComment-571828 Share on other sites More sharing options...
Barand Posted June 22, 2008 Share Posted June 22, 2008 $section = array_slice(array_values($this->data), 85, 10); Link to comment https://forums.phpfreaks.com/topic/111399-echoing-only-range-of-keyvalue-pairs-from-array-in-foreach-loop/#findComment-571838 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.