helpers.php 800 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/1/23
  6. * Time: 9:11 AM
  7. */
  8. function php2js($arr)
  9. {
  10. return json_encode($arr, true);
  11. }
  12. function js2php($str)
  13. {
  14. return json_decode($str, true);
  15. }
  16. function arr2option($arr)
  17. {
  18. return collect($arr)->map(function ($key, $val) {
  19. return [
  20. 'label' => $key,
  21. 'value' => $val,
  22. 's_value' => (string)$val
  23. ];
  24. })->values();
  25. }
  26. function str2arr($str, $glue = ',')
  27. {
  28. return explode($glue, $str);
  29. }
  30. function arr2str($arr, $glue = ',')
  31. {
  32. return implode($glue, array_wrap($arr));
  33. }
  34. function category($key, $option = false)
  35. {
  36. if ($option) {
  37. return \Encore\Category\Models\Category::byKeyGetOptions($key);
  38. }
  39. return \Encore\Category\Models\Category::byId($key);
  40. }