/system/helpers/array_helper.php CI列助手

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Array Helpers
 * 笨阵列助手
 * 
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/helpers/array_helper.html
 */

// ------------------------------------------------------------------------

/**
 * Element
 * 元素
 * 
 * Lets you determine whether an array index is set and whether it has a value.
 * If the element is empty it returns FALSE (or whatever you specify as the default value.)
 * 让你我是否数组索引设置,无论它有一个价值的确定性。
 * 如果该元素为空,则返回FALSE(或任何你指定的默认值。)
 * @access	public
 * @param	string
 * @param	array
 * @param	mixed
 * @return	mixed	depends on what the array contains
 */
//如果不存在element这个子数的话,创建一个
if ( ! function_exists('element'))
{
	//取得数组中指定元素的的值
	function element($item, $array, $default = FALSE)
	{
		//如果元素在数组中不存在,或者为空,直接返回默认值
		if ( ! isset($array[$item]) OR $array[$item] == "")
		{
			return $default;
		}

		return $array[$item];
	}
}

// ------------------------------------------------------------------------

/**
 * Random Element - Takes an array as input and returns a random element
 * 
 * 随机元素 - 注意到一个数组,作为输入并返回一个随机元素
 * 
 * @access	public
 * @param	array
 * @return	mixed	depends on what the array contains 取决于该数组包含什么
 */
if ( ! function_exists('random_element'))
{
   //取得数组中随机的元素  $array[array_rand($array)];
	function random_element($array)
	{
		if ( ! is_array($array))
		{
			return $array;
		}

		return $array[array_rand($array)];
	}
}

// --------------------------------------------------------------------

/**
 * Elements
 * 元素列表
 * 
 * Returns only the array items specified.  Will return a default value if
 * it is not set.
 * 返回指定的数组项目。将返回一个默认值,如果未设置。
 * @access	public
 * @param	array
 * @param	array
 * @param	mixed
 * @return	mixed	depends on what the array contains
 */
if ( ! function_exists('elements'))
{
	//$items: 
	//$array:
	//$default: 
	function elements($items, $array, $default = FALSE)
	{
		$return = array();
		//如果不为数组,直接转换为数组
		if ( ! is_array($items))
		{
			$items = array($items);
		}
		
		//开始循环元素
		foreach ($items as $item)
		{
			//如果该元素已经在数组中存在了,那么直接放入到$return数组中去
			if (isset($array[$item]))
			{
				$return[$item] = $array[$item];
			}
			else
			{
				//设置默认值
				$return[$item] = $default;
			}
		}
        //返回数组
		return $return;
	}
}

/* End of file array_helper.php */
/* Location: ./system/helpers/array_helper.php */

  

posted @ 2013-06-06 22:29  简单--生活  阅读(204)  评论(0)    收藏  举报
简单--生活(CSDN)