Andrey Andreev | e561733 | 2012-03-13 12:22:17 +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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 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 | e561733 | 2012-03-13 12:22:17 +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 | e561733 | 2012-03-13 12:22:17 +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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter Array Helpers |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Helpers |
| 33 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/helpers/array_helper.html |
| 36 | */ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | if ( ! function_exists('element')) |
| 41 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 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 | * @param string |
| 49 | * @param array |
| 50 | * @param mixed |
| 51 | * @return mixed depends on what the array contains |
| 52 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | function element($item, $array, $default = FALSE) |
| 54 | { |
Andrey Andreev | e561733 | 2012-03-13 12:22:17 +0200 | [diff] [blame] | 55 | return empty($array[$item]) ? $default : $array[$item]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 56 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // ------------------------------------------------------------------------ |
| 60 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | if ( ! function_exists('random_element')) |
| 62 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 63 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | function random_element($array) |
| 70 | { |
Andrey Andreev | e561733 | 2012-03-13 12:22:17 +0200 | [diff] [blame] | 71 | return is_array($array) ? $array[array_rand($array)] : $array; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 72 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 75 | // -------------------------------------------------------------------- |
| 76 | |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 77 | if ( ! function_exists('elements')) |
| 78 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 79 | /** |
| 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 | */ |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 90 | function elements($items, $array, $default = FALSE) |
| 91 | { |
| 92 | $return = array(); |
Andrey Andreev | e561733 | 2012-03-13 12:22:17 +0200 | [diff] [blame] | 93 | |
| 94 | is_array($items) OR $items = array($items); |
| 95 | |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 96 | foreach ($items as $item) |
| 97 | { |
Andrey Andreev | e561733 | 2012-03-13 12:22:17 +0200 | [diff] [blame] | 98 | $return[$item] = isset($array[$item]) ? $array[$item] : $default; |
Derek Jones | 3a082fd | 2010-10-07 09:38:55 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return $return; |
| 102 | } |
| 103 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | |
| 105 | /* End of file array_helper.php */ |
Andrey Andreev | e6f7d61 | 2012-03-20 15:39:22 +0200 | [diff] [blame] | 106 | /* Location: ./system/helpers/array_helper.php */ |