blob: 0e66e4b777a98d11b78ee1f0ab78a4071f88c928 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Array Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/array_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('element'))
42{
Timothy Warren01b129a2012-04-27 11:36:50 -040043 /**
44 * Element
45 *
46 * Lets you determine whether an array index is set and whether it has a value.
vlakoff0cd55352012-07-02 15:20:42 +020047 * If the element is empty it returns NULL (or whatever you specify as the default value.)
Timothy Warren01b129a2012-04-27 11:36:50 -040048 *
49 * @param string
50 * @param array
51 * @param mixed
52 * @return mixed depends on what the array contains
53 */
Phil Sturgeon3c993ba2012-07-01 23:46:24 +020054 function element($item, $array, $default = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Phil Sturgeon6801d042012-07-04 14:43:36 +020056 return array_key_exists($item, $array) ? $array[$item] : $default;
Barry Mienydd671972010-10-04 16:33:58 +020057 }
Derek Allard2067d1a2008-11-13 22:59:24 +000058}
59
60// ------------------------------------------------------------------------
61
Derek Allard2067d1a2008-11-13 22:59:24 +000062if ( ! function_exists('random_element'))
63{
Timothy Warren01b129a2012-04-27 11:36:50 -040064 /**
65 * Random Element - Takes an array as input and returns a random element
66 *
67 * @param array
68 * @return mixed depends on what the array contains
69 */
Derek Allard2067d1a2008-11-13 22:59:24 +000070 function random_element($array)
71 {
Andrey Andreeve5617332012-03-13 12:22:17 +020072 return is_array($array) ? $array[array_rand($array)] : $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
Derek Jones3a082fd2010-10-07 09:38:55 -050078if ( ! function_exists('elements'))
79{
Timothy Warren01b129a2012-04-27 11:36:50 -040080 /**
81 * Elements
82 *
83 * Returns only the array items specified. Will return a default value if
84 * it is not set.
85 *
86 * @param array
87 * @param array
88 * @param mixed
89 * @return mixed depends on what the array contains
90 */
Phil Sturgeon3c993ba2012-07-01 23:46:24 +020091 function elements($items, $array, $default = NULL)
Derek Jones3a082fd2010-10-07 09:38:55 -050092 {
93 $return = array();
Andrey Andreeve5617332012-03-13 12:22:17 +020094
95 is_array($items) OR $items = array($items);
96
Derek Jones3a082fd2010-10-07 09:38:55 -050097 foreach ($items as $item)
98 {
Phil Sturgeon6801d042012-07-04 14:43:36 +020099 $return[$item] = array_key_exists($item, $array) ? $array[$item] : $default;
Derek Jones3a082fd2010-10-07 09:38:55 -0500100 }
101
102 return $return;
103 }
104}
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
106/* End of file array_helper.php */
Andrey Andreeve6f7d612012-03-20 15:39:22 +0200107/* Location: ./system/helpers/array_helper.php */