99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

CodeIgniter 數(shù)組輔助函數(shù)

2018-07-21 15:42 更新

數(shù)組輔助函數(shù)

數(shù)組輔助函數(shù)文件包含了一些幫助你處理數(shù)組的函數(shù)。

加載輔助函數(shù)

該輔助函數(shù)通過下面的代碼加載:

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

可用函數(shù)

該輔助函數(shù)有下列可用函數(shù):

element($item, $array[, $default = NULL])

參數(shù):

  • $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

該函數(shù)通過索引獲取數(shù)組中的元素。它會測試索引是否設(shè)置并且有值,如果有值, 函數(shù)將返回該值,如果沒有值,默認(rèn)返回 NULL 或返回通過第三個參數(shù)設(shè)置的默認(rèn)值。

示例:

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

echo element('color', $array); // returns "red"
echo element('size', $array, 'foobar'); // returns "foobar"

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

參數(shù):

  • $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

該函數(shù)通過多個索引獲取數(shù)組中的多個元素。它會測試每一個索引是否設(shè)置并且有值, 如果其中某個索引沒有值,返回結(jié)果中該索引所對應(yīng)的元素將被置為 NULL ,或者 通過第三個參數(shù)設(shè)置的默認(rèn)值。

示例:

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

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

上面的函數(shù)返回的結(jié)果如下:

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

你可以通過第三個參數(shù)設(shè)置任何你想要設(shè)置的默認(rèn)值。

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

上面的函數(shù)返回的結(jié)果如下:

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

當(dāng)你需要將 $_POST 數(shù)組傳遞到你的模型中時這將很有用,這可以防止用戶發(fā)送額外的數(shù)據(jù) 被寫入到你的數(shù)據(jù)庫。

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

從上例中可以看出,只有 id、title、content 三個字段被更新。

random_element($array)

參數(shù):

  • $array (array) -- Input array

返回: A random element from the array

返回類型: mixed

傳入一個數(shù)組,并返回數(shù)組中隨機(jī)的一個元素。

使用示例:

$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);
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號