CI辅助函数参考

一、数组辅助函数

该辅助函数通过下面的代码加载:

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

1、elements($items$array[, $default = NULL])

参数:
  • $item (string) -- Item to fetch from the array
  • $array (array) -- Input array
  • $default (bool) -- What to return if the array isn't valid
返回:

NULL on failure or the array item.

返回类型:

mixed

该函数通过多个索引获取数组中的多个元素。它会测试每一个索引是否设置并且有值, 如果其中某个索引没有值,返回结果中该索引所对应的元素将被置为 NULL ,或者 通过第三个参数设置的默认值。

示例:

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

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

上面的函数返回的结果如下:

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

你可以通过第三个参数设置任何你想要设置的默认值。

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

上面的函数返回的结果如下:

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

当你需要将 $_POST 数组传递到你的模型中时这将很有用,这可以防止用户发送额外的数据 被写入到你的数据库。

从上例中可以看出,只有数据库存在的字段才被更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值