blob: 6b8695c11cf0aa31db55523d8642dd262adf2efb [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Array Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/array_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Element
44 *
45 * Lets you determine whether an array index is set and whether it has a value.
46 * If the element is empty it returns FALSE (or whatever you specify as the default value.)
47 *
48 * @access public
49 * @param string
50 * @param array
51 * @param mixed
52 * @return mixed depends on what the array contains
Barry Mienydd671972010-10-04 16:33:58 +020053 */
Derek Allard2067d1a2008-11-13 22:59:24 +000054if ( ! function_exists('element'))
55{
56 function element($item, $array, $default = FALSE)
57 {
58 if ( ! isset($array[$item]) OR $array[$item] == "")
59 {
60 return $default;
61 }
62
63 return $array[$item];
Barry Mienydd671972010-10-04 16:33:58 +020064 }
Derek Allard2067d1a2008-11-13 22:59:24 +000065}
66
67// ------------------------------------------------------------------------
68
69/**
70 * Random Element - Takes an array as input and returns a random element
71 *
72 * @access public
73 * @param array
74 * @return mixed depends on what the array contains
Barry Mienydd671972010-10-04 16:33:58 +020075 */
Derek Allard2067d1a2008-11-13 22:59:24 +000076if ( ! function_exists('random_element'))
77{
78 function random_element($array)
79 {
80 if ( ! is_array($array))
81 {
82 return $array;
83 }
Robin Sowelle8867712011-01-30 17:30:54 -050084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 return $array[array_rand($array)];
Barry Mienydd671972010-10-04 16:33:58 +020086 }
Derek Allard2067d1a2008-11-13 22:59:24 +000087}
88
Derek Jones3a082fd2010-10-07 09:38:55 -050089// --------------------------------------------------------------------
90
91/**
92 * Elements
93 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050094 * Returns only the array items specified. Will return a default value if
Derek Jones3a082fd2010-10-07 09:38:55 -050095 * it is not set.
96 *
97 * @access public
98 * @param array
99 * @param array
100 * @param mixed
101 * @return mixed depends on what the array contains
102 */
103if ( ! function_exists('elements'))
104{
105 function elements($items, $array, $default = FALSE)
106 {
107 $return = array();
Derek Jones37f4b9c2011-07-01 17:56:50 -0500108
Derek Jones3a082fd2010-10-07 09:38:55 -0500109 if ( ! is_array($items))
110 {
111 $items = array($items);
112 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500113
Derek Jones3a082fd2010-10-07 09:38:55 -0500114 foreach ($items as $item)
115 {
116 if (isset($array[$item]))
117 {
118 $return[$item] = $array[$item];
119 }
120 else
121 {
122 $return[$item] = $default;
123 }
124 }
125
126 return $return;
127 }
128}
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
130/* End of file array_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000131/* Location: ./system/helpers/array_helper.php */