Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Возвращает соседние элементы массива
- * @param $arr - массив, в котором нужны элементы
- * @param $key - центральный ключ, соседи которого нужно получить
- * @return array - ['previous' => $key, 'next' => $key]
- */
- private function arrayNeighbor($arr, $key)
- {
- krsort($arr);
- $keys = array_keys($arr);
- $keyIndexes = array_flip($keys);
- $return = array();
- if (isset($keys[$keyIndexes[$key]-1])) {
- $return['next'] = $keys[$keyIndexes[$key]-1];
- }
- if (isset($keys[$keyIndexes[$key]+1])) {
- $return['previous'] = $keys[$keyIndexes[$key]+1];
- }
- if (is_null($return['previous'])) {
- $return['previous'] = $keys[0];
- }
- if (is_null($return['next'])) {
- $return['next'] = end($keys);
- }
- return $return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement