blob: 447ee1aa4d799062472c20dbab71f8943a429afd [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Array Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/array_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Element
32 *
33 * Lets you determine whether an array index is set and whether it has a value.
34 * If the element is empty it returns FALSE (or whatever you specify as the default value.)
35 *
36 * @access public
37 * @param string
38 * @param array
39 * @param mixed
40 * @return mixed depends on what the array contains
Barry Mienydd671972010-10-04 16:33:58 +020041 */
Derek Allard2067d1a2008-11-13 22:59:24 +000042if ( ! function_exists('element'))
43{
44 function element($item, $array, $default = FALSE)
45 {
46 if ( ! isset($array[$item]) OR $array[$item] == "")
47 {
48 return $default;
49 }
50
51 return $array[$item];
Barry Mienydd671972010-10-04 16:33:58 +020052 }
Derek Allard2067d1a2008-11-13 22:59:24 +000053}
54
55// ------------------------------------------------------------------------
56
57/**
58 * Random Element - Takes an array as input and returns a random element
59 *
60 * @access public
61 * @param array
62 * @return mixed depends on what the array contains
Barry Mienydd671972010-10-04 16:33:58 +020063 */
Derek Allard2067d1a2008-11-13 22:59:24 +000064if ( ! function_exists('random_element'))
65{
66 function random_element($array)
67 {
68 if ( ! is_array($array))
69 {
70 return $array;
71 }
72 return $array[array_rand($array)];
Barry Mienydd671972010-10-04 16:33:58 +020073 }
Derek Allard2067d1a2008-11-13 22:59:24 +000074}
75
Derek Jones3a082fd2010-10-07 09:38:55 -050076// --------------------------------------------------------------------
77
78/**
79 * Elements
80 *
81 * Returns only the array items specified. Will return a default value if
82 * it is not set.
83 *
84 * @access public
85 * @param array
86 * @param array
87 * @param mixed
88 * @return mixed depends on what the array contains
89 */
90if ( ! function_exists('elements'))
91{
92 function elements($items, $array, $default = FALSE)
93 {
94 $return = array();
95
96 if ( ! is_array($items))
97 {
98 $items = array($items);
99 }
100
101 foreach ($items as $item)
102 {
103 if (isset($array[$item]))
104 {
105 $return[$item] = $array[$item];
106 }
107 else
108 {
109 $return[$item] = $default;
110 }
111 }
112
113 return $return;
114 }
115}
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117/* End of file array_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000118/* Location: ./system/helpers/array_helper.php */