How does the CI framework operate arrays? CI framework array operation skills. Array auxiliary functions

Source: Internet
Author: User
An example of the CI framework is to illustrate some array techniques. CIPHP is an example of the CI reprinting framework from CI China. it is to illustrate some array techniques. ci php is from CI China.

The array auxiliary function file contains some functions used to assist in array operations.

Attach this auxiliary function

The following code is used to load the auxiliary function:

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

The available functions are as follows:

Element ()

Obtain the elements in the array. This function tests whether the index of the array has been set and contains a value. If a value is set, this value is returned. otherwise, FALSE is returned, or any default value you set (the third parameter of the function ). Example:

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

// Return "red"
Echo element ('color', $ array );

// Return NULL
Echo element ('size', $ array, NULL );


Random_element ()

Returns an element of the array randomly based on the provided array. Example:

$ 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 insertion Sion, albeit a very persistent one.-Albert Stein ",
"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:

This function retrieves several elements from an array. This function tests whether each key value in the (input) array is defined in the (target) array. if a key value does not exist, the value corresponding to the key value is set to FALSE, or you can specify the default value by passing in the 3rd parameters. For example:

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

The above program will return the following array:

Array (
'Color' => 'red ',
'Shape' => 'round ',
'Height' => FALSE
);

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

You can set 3rd parameters to any default value you want:

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

The above will return the following array:

The above program will return the following array:

Array (
'Color' => 'red ',
'Shape' => 'round ',
'Height' => NULL
);

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

This (method) is useful when passing the $ _ POST array into your model. This method prevents extra POST data sent by users from entering your data table:

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

This ensures that only the id, title, and content fields are sent for update.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.