blob: 881f57db31cf57b831de40edcbc320381cd22e28 [file] [log] [blame]
Andrey Andreevf1b43d42012-01-06 14:41:50 +02001<?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
Andrey Andreevf1b43d42012-01-06 14:41:50 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevf1b43d42012-01-06 14:41:50 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020080 return (is_array($array)) ? $array[array_rand($array)] : $array;
Barry Mienydd671972010-10-04 16:33:58 +020081 }
Derek Allard2067d1a2008-11-13 22:59:24 +000082}
83
Derek Jones3a082fd2010-10-07 09:38:55 -050084// --------------------------------------------------------------------
85
86/**
87 * Elements
88 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050089 * Returns only the array items specified. Will return a default value if
Derek Jones3a082fd2010-10-07 09:38:55 -050090 * it is not set.
91 *
92 * @access public
93 * @param array
94 * @param array
95 * @param mixed
96 * @return mixed depends on what the array contains
97 */
98if ( ! function_exists('elements'))
99{
100 function elements($items, $array, $default = FALSE)
101 {
102 $return = array();
Derek Jones3a082fd2010-10-07 09:38:55 -0500103 if ( ! is_array($items))
104 {
105 $items = array($items);
106 }
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200107
Derek Jones3a082fd2010-10-07 09:38:55 -0500108 foreach ($items as $item)
109 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200110 $return[$item] = (isset($array[$item])) ? $array[$item] : $default;
Derek Jones3a082fd2010-10-07 09:38:55 -0500111 }
112
113 return $return;
114 }
115}
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117/* End of file array_helper.php */
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200118/* Location: ./system/helpers/array_helper.php */