12345678910111213141516171819202122232425262728293031323334353637383940 |
- <?php
-
- namespace App\Http\Helper;
-
- class Helper {
-
- /**
- * Summary of findNextStepsIDs
- * @param array $arr 目標陣列
- * @param mixed $targetID 指定的value
- * @param int $count 尋找組數
- * @param string $direction 尋找方向 up down
- * @return array
- */
- function findArrayTargetIndex(array $arr, $target, int $count, string $direction = "down") {
- // 找到指定value 的Index
- $targetIndex = array_search($target, $arr);
-
- // 如果指定的value不存在陣列中,則回傳空陣列
- if ($targetIndex === false) {
- return [];
- }
-
- $result = [];
- //計算陣列的數量
- $arrayLength = count($arr);
-
- // 根据方向确定增量
- $increment = $direction === 'down' ? 1 : -1;
-
- // 从目标索引开始找指定数量的元素
- for ($i = 1; $i <= $count; $i++) {
- $currentIndex = ($targetIndex + $increment * $i + $arrayLength) % $arrayLength;
- $result[] = $arr[$currentIndex];
- }
-
- return $result;
- }
- }
|