array_helper.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter Array Helpers
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Helpers
  22. * @category Helpers
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/helpers/array_helper.html
  25. */
  26. // ------------------------------------------------------------------------
  27. /**
  28. * Element
  29. *
  30. * Lets you determine whether an array index is set and whether it has a value.
  31. * If the element is empty it returns FALSE (or whatever you specify as the default value.)
  32. *
  33. * @access public
  34. * @param string
  35. * @param array
  36. * @param mixed
  37. * @return mixed depends on what the array contains
  38. */
  39. if ( ! function_exists('element'))
  40. {
  41. function element($item, $array, $default = FALSE)
  42. {
  43. if ( ! isset($array[$item]) OR $array[$item] == "")
  44. {
  45. return $default;
  46. }
  47. return $array[$item];
  48. }
  49. }
  50. // ------------------------------------------------------------------------
  51. /**
  52. * Random Element - Takes an array as input and returns a random element
  53. *
  54. * @access public
  55. * @param array
  56. * @return mixed depends on what the array contains
  57. */
  58. if ( ! function_exists('random_element'))
  59. {
  60. function random_element($array)
  61. {
  62. if ( ! is_array($array))
  63. {
  64. return $array;
  65. }
  66. return $array[array_rand($array)];
  67. }
  68. }
  69. // --------------------------------------------------------------------
  70. /**
  71. * Elements
  72. *
  73. * Returns only the array items specified. Will return a default value if
  74. * it is not set.
  75. *
  76. * @access public
  77. * @param array
  78. * @param array
  79. * @param mixed
  80. * @return mixed depends on what the array contains
  81. */
  82. if ( ! function_exists('elements'))
  83. {
  84. function elements($items, $array, $default = FALSE)
  85. {
  86. $return = array();
  87. if ( ! is_array($items))
  88. {
  89. $items = array($items);
  90. }
  91. foreach ($items as $item)
  92. {
  93. if (isset($array[$item]))
  94. {
  95. $return[$item] = $array[$item];
  96. }
  97. else
  98. {
  99. $return[$item] = $default;
  100. }
  101. }
  102. return $return;
  103. }
  104. }
  105. /* End of file array_helper.php */
  106. /* Location: ./system/helpers/array_helper.php */