目录 搜索
欢迎 目录 快速参考图 基本信息 服务器要求 许可协议 变更记录 关于CodeIgniter 安装 下载 CodeIgniter 安装指导 从老版本升级 疑难解答 介绍 开始 CodeIgniter 是什么? CodeIgniter 速记表 支持特性 应用程序流程图 模型-视图-控制器 架构目标 教程 内容提要 加载静态内容 创建新闻条目 读取新闻条目 结束语 常规主题 CodeIgniter URL 控制器 保留字 视图 模型 辅助函数 使用 CodeIgniter 类库 创建你自己的类库 使用 CodeIgniter 适配器 创建适配器 创建核心系统类 钩子 - 扩展框架的核心 自动装载资源 公共函数 URI 路由 错误处理 缓存 调试应用程序 以CLI方式运行 管理应用程序 处理多环境 PHP替代语法 安全 开发规范 类库参考 基准测试类 日历类 购物车类 配置类 Email 类 加密类 文件上传类 表单验证详解 FTP 类 图像处理类 输入类 Javascript 类 语言类 装载类 迁移类 输出类 分页类 模板解析器类 安全类 Session 类 HTML 表格类 引用通告类 排版类 单元测试类 URI 类 User-Agent 类 表单验证 XML-RPC 和 XML-RPC 服务器 Zip 编码类 缓存适配器 适配器参考 适配器 数据库类 Active Record 类 数据库缓存类 自定义函数调用 数据库配置 连接你的数据库 数据库快速入门例子代码 字段数据 数据库维护类 查询辅助函数 数据库类 查询 生成查询记录集 表数据 事务 数据库工具类 JavaScript类 辅助函数参考 数组辅助函数 CAPTCHA 辅助函数 Cookie Helper 日期辅助函数 目录辅助函数 下载辅助函数 Email 辅助函数 文件辅助函数 表单辅助函数 HTML辅助函数 Inflector 辅助函数 语言辅助函数 数字辅助函数 路径辅助函数 安全辅助函数 表情辅助函数 字符串辅助函数 文本辅助函数 排版辅助函数 URL 辅助函数 XML 辅助函数
文字

CodeIgniter 用户指南 版本 2.1.0

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

数组辅助函数

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

装载本辅助函数

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

$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);

elements()

Lets you fetch a number of items from an array. The function tests whether each of the array indices is set. If an index does not exist it is set to FALSE, or whatever you've specified as the default value via the third parameter. Example:

试译:该函数从一个数组中取得若干元素。该函数测试(传入)数组的每个键值是否在(目标)数组中已定义;如果一个键值不存在,该键值所对应的值将被置为FALSE,或者你可以通过传入的第3个参数来指定默认的值。例如:

$array = array(
    'color' => 'red',
    'shape' => 'round',
    'radius' => '10',
    'diameter' => '20'
);

$my_shape = elements(array('color', 'shape', 'height'), $array);

The above will return the following array:

试译:上面的程序将返回下面的数组:

array(
    'color' => 'red',
    'shape' => 'round',
    'height' => FALSE
);

You can set the third parameter to any default value you like:

试译:你可以将第3个参数设为任何你想要的默认值:

$my_shape = elements(array('color', 'shape', 'height'), $array, NULL);

The above will return the following array:

试译:上面的程序将返回下面的数组:

array(
    'color' => 'red',
    'shape' => 'round',
    'height' => NULL
);

This is useful when sending the $_POST array to one of your Models. This prevents users from sending additional POST data to be entered into your tables:

试译:这(种方法)在将 $_POST 数组传入你的模型时非常有用。通过这种方式可以防止用户发送的额外的 POST 数据进入你的数据表:

$this->load->model('post_model');

$this->post_model->update(elements(array('id', 'title', 'content'), $_POST));

This ensures that only the id, title and content fields are sent to be updated.

试译:这样保证了只有 id, title 和 content 字段被发送以进行更新。

 

翻译贡献者: canglan, Hex, zhaosusen
最后修改: 2012-02-05 23:45:13
上一篇: 下一篇: