Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame^] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 53 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | if ( ! 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | } |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | if ( ! function_exists('random_element')) |
| 77 | { |
| 78 | function random_element($array) |
| 79 | { |
| 80 | if ( ! is_array($array)) |
| 81 | { |
| 82 | return $array; |
| 83 | } |
Robin Sowell | e886771 | 2011-01-30 17:30:54 -0500 | [diff] [blame] | 84 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | return $array[array_rand($array)]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 89 | // -------------------------------------------------------------------- |
| 90 | |
| 91 | /** |
| 92 | * Elements |
| 93 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 94 | * Returns only the array items specified. Will return a default value if |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 95 | * 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 | */ |
| 103 | if ( ! function_exists('elements')) |
| 104 | { |
| 105 | function elements($items, $array, $default = FALSE) |
| 106 | { |
| 107 | $return = array(); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 108 | |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 109 | if ( ! is_array($items)) |
| 110 | { |
| 111 | $items = array($items); |
| 112 | } |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 113 | |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 114 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | |
| 130 | /* End of file array_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 131 | /* Location: ./system/helpers/array_helper.php */ |