helpers.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. if (!function_exists('array_dot')) {
  3. /**
  4. * Flatten a multi-dimensional associative array with dots.
  5. *
  6. * @param array $array The array to flatten.
  7. * @param string $rootKey The base key prefix (used internally for recursion).
  8. * @return array The flattened array with dot notation keys.
  9. */
  10. function array_dot(array $array, string $rootKey = ''): array
  11. {
  12. $result = [];
  13. foreach ($array as $key => $value) {
  14. $key = strval($key);
  15. $key = $rootKey !== '' ? ($rootKey . '.' . $key) : $key;
  16. if (is_array($value)) {
  17. $result = $result + array_dot($value, $key);
  18. continue;
  19. }
  20. $result[$key] = $value;
  21. }
  22. return $result;
  23. }
  24. }
  25. if (!function_exists('str_starts_with')) {
  26. /**
  27. * @param string $haystack
  28. * @param string $needle
  29. * @return bool
  30. */
  31. function str_starts_with(string $haystack, string $needle): bool
  32. {
  33. return substr($haystack, 0, strlen($needle)) === $needle;
  34. }
  35. }