Skip to content

Instantly share code, notes, and snippets.

@mahdiyazdani
Created October 26, 2020 13:52
Show Gist options
  • Save mahdiyazdani/a57686e0f39cb98e24d1b4841a03342c to your computer and use it in GitHub Desktop.
Save mahdiyazdani/a57686e0f39cb98e24d1b4841a03342c to your computer and use it in GitHub Desktop.
Retrieve the next key in an array from given key
<?php
/**
* Retrieve the next key in an array from given key.
*
* @since 1.0.0
* @param array $arr List of items in a form of array.
* @param integer $key Key to search and find within the array.
* @return integer
*/
function prefix_get_next_key_array( $arr, $key ) {
$keys = array_keys( $arr );
$position = array_search( $key, $keys, true );
if ( isset( $keys[ $position + 1 ] ) ) {
$next_key = $keys[ $position + 1 ];
}
return $next_key;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment