dadamssg87 Posted September 18, 2012 Share Posted September 18, 2012 If i'm looping through an array how do i get the next item key. For example: $items['a'] = "One"; $items['b'] = "Two"; $items['c'] = "Three"; $items['d'] = "Four"; foreach($items as $key => $value){ $next_item = next($items); $next_key = key($next_item); echo "The current value is $value. The next key is $next_key.<br/>"; } $next_item is returned as a string. The key() function is failing. Link to comment https://forums.phpfreaks.com/topic/268486-get-next-item-key-in-array-loop/ Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 next returns a string, not the array. What you should probably do is use array_keys and loop through that. Link to comment https://forums.phpfreaks.com/topic/268486-get-next-item-key-in-array-loop/#findComment-1378781 Share on other sites More sharing options...
trq Posted September 18, 2012 Share Posted September 18, 2012 There is likely a better way. What exactly are you doing? Link to comment https://forums.phpfreaks.com/topic/268486-get-next-item-key-in-array-loop/#findComment-1378782 Share on other sites More sharing options...
dadamssg87 Posted September 18, 2012 Author Share Posted September 18, 2012 pretty much exactly what i'm doing with my echo statement. I just want to get the key of the next item in the array while in the loop. Link to comment https://forums.phpfreaks.com/topic/268486-get-next-item-key-in-array-loop/#findComment-1378786 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 Why? Link to comment https://forums.phpfreaks.com/topic/268486-get-next-item-key-in-array-loop/#findComment-1378787 Share on other sites More sharing options...
dadamssg87 Posted September 18, 2012 Author Share Posted September 18, 2012 I'm trying to create a page that has several full blog posts. Each post will be echo'ed out within a loop. I want each blog title to have an anchor next to it. Something like "<a name="post_<?=$post_id;?>" /> under each blog title i want a link that links to the next blog title "<a href="#post_<?=$next_post_id;?>">Not interested? Skip to next article.</a> Link to comment https://forums.phpfreaks.com/topic/268486-get-next-item-key-in-array-loop/#findComment-1378788 Share on other sites More sharing options...
Christian F. Posted September 18, 2012 Share Posted September 18, 2012 In that case I'd actually construct the items array like this: $items = array (array ('a', 'one'), array ('b', 'two'), array ('c', 'three')); That said, you can actually increase the letters as if they were number too. So you can do this, though I wouldn't recommend it: foreach($items as $key => $value){ // Increment the key until next valid index is found. while (!isset ($items[++$key])); echo "The current value is $value. The next key is $key.<br/>"; } Link to comment https://forums.phpfreaks.com/topic/268486-get-next-item-key-in-array-loop/#findComment-1378829 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 What you should probably do is use array_keys() Link to comment https://forums.phpfreaks.com/topic/268486-get-next-item-key-in-array-loop/#findComment-1378879 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.