Helper.php 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Http\Helper;
  3. class Helper {
  4. /**
  5. * Summary of findNextStepsIDs
  6. * @param array $arr 目標陣列
  7. * @param mixed $targetID 指定的value
  8. * @param int $count 尋找組數
  9. * @param string $direction 尋找方向 up down
  10. * @return array
  11. */
  12. function findArrayTargetIndex(array $arr, $target, int $count, string $direction = "down") {
  13. // 找到指定value 的Index
  14. $targetIndex = array_search($target, $arr);
  15. // 如果指定的value不存在陣列中,則回傳空陣列
  16. if ($targetIndex === false) {
  17. return [];
  18. }
  19. $result = [];
  20. //計算陣列的數量
  21. $arrayLength = count($arr);
  22. // 根据方向确定增量
  23. $increment = $direction === 'down' ? 1 : -1;
  24. // 从目标索引开始找指定数量的元素
  25. for ($i = 1; $i <= $count; $i++) {
  26. $currentIndex = ($targetIndex + $increment * $i + $arrayLength) % $arrayLength;
  27. $result[] = $arr[$currentIndex];
  28. }
  29. return $result;
  30. }
  31. }