Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [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 |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame^] | 80 | return (is_array($array)) ? $array[array_rand($array)] : $array; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
| 87 | * Elements |
| 88 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 89 | * Returns only the array items specified. Will return a default value if |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 90 | * 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 | */ |
| 98 | if ( ! function_exists('elements')) |
| 99 | { |
| 100 | function elements($items, $array, $default = FALSE) |
| 101 | { |
| 102 | $return = array(); |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 103 | if ( ! is_array($items)) |
| 104 | { |
| 105 | $items = array($items); |
| 106 | } |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame^] | 107 | |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 108 | foreach ($items as $item) |
| 109 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame^] | 110 | $return[$item] = (isset($array[$item])) ? $array[$item] : $default; |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return $return; |
| 114 | } |
| 115 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | |
| 117 | /* End of file array_helper.php */ |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame^] | 118 | /* Location: ./system/helpers/array_helper.php */ |