array_helper.php 2.5 KB

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