blob: 216f12e567917778d94f8c91018d0eb5940359ee [file] [log] [blame]
Andrey Andreeve5617332012-03-13 12:22:17 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreeve5617332012-03-13 12:22:17 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreeve5617332012-03-13 12:22:17 +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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Array Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/helpers/array_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('element'))
41{
Timothy Warren01b129a2012-04-27 11:36:50 -040042 /**
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 * @param string
49 * @param array
50 * @param mixed
51 * @return mixed depends on what the array contains
52 */
Phil Sturgeon3c993ba2012-07-01 23:46:24 +020053 function element($item, $array, $default = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
Andrey Andreeve5617332012-03-13 12:22:17 +020055 return empty($array[$item]) ? $default : $array[$item];
Barry Mienydd671972010-10-04 16:33:58 +020056 }
Derek Allard2067d1a2008-11-13 22:59:24 +000057}
58
59// ------------------------------------------------------------------------
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061if ( ! function_exists('random_element'))
62{
Timothy Warren01b129a2012-04-27 11:36:50 -040063 /**
64 * Random Element - Takes an array as input and returns a random element
65 *
66 * @param array
67 * @return mixed depends on what the array contains
68 */
Derek Allard2067d1a2008-11-13 22:59:24 +000069 function random_element($array)
70 {
Andrey Andreeve5617332012-03-13 12:22:17 +020071 return is_array($array) ? $array[array_rand($array)] : $array;
Barry Mienydd671972010-10-04 16:33:58 +020072 }
Derek Allard2067d1a2008-11-13 22:59:24 +000073}
74
Derek Jones3a082fd2010-10-07 09:38:55 -050075// --------------------------------------------------------------------
76
Derek Jones3a082fd2010-10-07 09:38:55 -050077if ( ! function_exists('elements'))
78{
Timothy Warren01b129a2012-04-27 11:36:50 -040079 /**
80 * Elements
81 *
82 * Returns only the array items specified. Will return a default value if
83 * it is not set.
84 *
85 * @param array
86 * @param array
87 * @param mixed
88 * @return mixed depends on what the array contains
89 */
Phil Sturgeon3c993ba2012-07-01 23:46:24 +020090 function elements($items, $array, $default = NULL)
Derek Jones3a082fd2010-10-07 09:38:55 -050091 {
92 $return = array();
Andrey Andreeve5617332012-03-13 12:22:17 +020093
94 is_array($items) OR $items = array($items);
95
Derek Jones3a082fd2010-10-07 09:38:55 -050096 foreach ($items as $item)
97 {
Andrey Andreeve5617332012-03-13 12:22:17 +020098 $return[$item] = isset($array[$item]) ? $array[$item] : $default;
Derek Jones3a082fd2010-10-07 09:38:55 -050099 }
100
101 return $return;
102 }
103}
Derek Allard2067d1a2008-11-13 22:59:24 +0000104
105/* End of file array_helper.php */
Andrey Andreeve6f7d612012-03-20 15:39:22 +0200106/* Location: ./system/helpers/array_helper.php */