?
This document uses PHP Chinese website manual Release
Array Helper文件包含有助于處理數(shù)組的函數(shù)。
加載此助手
可用職能
使用以下代碼加載此助手:
$this->load->helper('array');
現(xiàn)有下列職能:
element($item, $array[, $default = NULL])
參數(shù): | $ item(string) - 從數(shù)組中獲取的項(xiàng)目$ array(array) - 輸入數(shù)組$ default(bool) - 如果數(shù)組無效 |
---|---|
返回: | NULL失敗或數(shù)組項(xiàng)。 |
返回類型: | 雜 |
$item (string) -從數(shù)組中獲取的項(xiàng)
$array (array) -輸入數(shù)組
$ default(bool) - 如果數(shù)組無效應(yīng)該返回什么。
返回:失敗或數(shù)組項(xiàng)時(shí)返回NULL。
返回類型:混合
讓您從數(shù)組中獲取項(xiàng)目。該函數(shù)測試數(shù)組索引是否已設(shè)置以及是否有值。如果存在值,則返回該值。如果值不存在,則返回NULL,或者通過第三個(gè)參數(shù)指定為默認(rèn)值。
例子:
$ array = array('color'=>'red','shape'=>'round','size'=>'');
echo元素('color',$ array); //返回“紅色”
echo元素('size',$ array,'foobar'); //返回“foobar”
elements($items, $array[, $default = NULL])
參數(shù): | $ item(string) - 從數(shù)組中獲取的項(xiàng)目$ array(array) - 輸入數(shù)組$ default(bool) - 如果數(shù)組無效 |
---|---|
返回: | NULL失敗或數(shù)組項(xiàng)。 |
返回類型: | 雜 |
$ item(string) - 從數(shù)組中獲取的項(xiàng)目
$ array(array) - 輸入數(shù)組
$ default(bool) - 如果數(shù)組無效那么返回什么
返回:失敗或數(shù)組項(xiàng)時(shí)返回NULL。
返回類型:混合
讓您從數(shù)組中獲取多個(gè)項(xiàng)目。該函數(shù)測試是否設(shè)置每個(gè)數(shù)組索引。如果索引不存在,它將被設(shè)置為NULL,或者通過第三個(gè)參數(shù)指定為默認(rèn)值。
例子:
$ array = array('color'=>'red','shape'=>'round','radius'=>'10','diameter'=>'20'); $ my_shape = elements(array('color','shape','height'),$ array);
以上將返回以下數(shù)組:
array('color'=>'red','shape'=>'round','height'=> NULL);
您可以將第三個(gè)參數(shù)設(shè)置為您喜歡的任何默認(rèn)值。
$ my_shape = elements(array('color','shape','height'),$ array,'foobar');
以上將返回以下數(shù)組:
數(shù)組('color'=>'red','shape'=>'round','height'=>'foobar');
將$_POST
數(shù)組發(fā)送到其中一個(gè)模型時(shí)非常有用。這可以防止用戶發(fā)送額外的POST數(shù)據(jù)輸入到表中。
$this - >負(fù)載>模型( 'post_model'); $ this-> post_model-> update(elements(array('id','title','content'),$ _POST));
這確保只有id,標(biāo)題和內(nèi)容字段被發(fā)送來更新。
random_element($array)
參數(shù): | $ array(array) - 輸入數(shù)組 |
---|---|
返回: | 數(shù)組中的隨機(jī)元素 |
返回類型: | 雜 |
$ array(array) - 輸入數(shù)組
返回:數(shù)組中的一個(gè)隨機(jī)元素
返回類型:混合
將數(shù)組作為輸入并從中返回一個(gè)隨機(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);