CodeIgniter 用户指南 版本 1.7.2

编辑文档、查看近期更改请 登录注册  找回密码
查看原文

数组辅助函数

数组辅助函数的文件涵盖了一些用于辅助数组操作的函数。

装载本辅助函数

本辅助函数的装载通过如下代码完成:

$this->load->helper('array');

可用的函数如下:

element()

获取数组中的元素。本函数测试数组的索引是否已设定并含有数值。如果已设有数值则返回该数值,否则返回 FALSE,或任何你设定的默认数值(函数第三个参数)。范例:

$array = array('color' => 'red', 'shape' => 'round', 'size' => '');

// 返回 "red"
echo element('color', $array);

// 返回 NULL
echo element('size', $array, NULL);

random_element()

根据提供的数组,随机返回该数组内的一个元素。使用范例:

$quotes = array(
            "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
            "Don't stay in bed, unless you can make money in bed. - George Burns",
            "We didn't lose the game; we just ran out of time. - Vince Lombardi",
            "If everything seems under control, you're not going fast enough. - Mario Andretti",
            "Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
            "Chance favors the prepared mind - Louis Pasteur"
            );

echo random_element($quotes);

 

翻译贡献者: canglan, Hex
最后修改: 2008-06-28 13:04:32