How to use php to retrieve data of the specified length in an array

Source: Internet
Author: User
How to use php to retrieve data of the specified length in an array
This article introduces how to obtain data of the specified length in the array in php programming. For more information, see.

In the php program, reducing the number of queries is also a way to improve the overall performance of the website.

Query all the data that meets the basic conditions:

     AccessCourse ('Id DESC ', 1); // query the data that meets the basic conditions. The status is normal... $ list = Json_decode ($ list_json, true); // Convert the queried json format to array format. // popular courses $ toplist = array_sort ($ list, 'num', SORT_DESC); // Two-dimensional array sorting $ this-> assign ('toplist', $ toplist); // $ interest = findArrayList ($ list, 9); // filter out 9 pieces of data $ this-> assign ('interest ', $ interest); // listen to the course $ listen_yw = findArrayList ($ list, 4, 'subobject', 1); // Language $ listen_sx = findArrayList ($ list, 4, 'subje CT ', 2); // Mathematics $ listen_yy = findArrayList ($ list, 4, 'subobject', 3); // English $ listen_wl = findArrayList ($ list, 4, 'subobject', 4); // physical $ listen_hx = findArrayList ($ list, 4, 'subobject', 5 ); // chemistry $ this-> assign ('Listen _ yw', $ listen_yw); $ this-> assign ('Listen _ sx ', $ listen_sx ); $ this-> assign ('Listen _ yy', $ listen_yy); $ this-> assign ('Listen _ wl ', $ listen_wl ); $ this-> assign ('Listen _ hx ', $ listen_hx);?>

The following is the implementation code of the findArrayList function.

     $ V) {if ($ array [$ k] [$ field] ==$ value) {$ new_arr [$ k] = $ v ;}} if (empty ($ new_arr) {return ;}} if (empty ($ new_arr) {$ new_arr = $ array;} if (! Is_null ($ length) {$ new_arr = array_slice ($ new_arr, 0, $ length); // Retrieve the length specified in the array} return $ new_arr;}?>

Code description: array_slice extracts an array array_slice (array, int offset [, int length [, bool preserve_keys]) array_slice () from the array () returns a sequence in the array specified by the offset and length parameters.

If offset is not negative, the sequence starts from this offset in array. If offset is negative, the sequence starts from the end of the array.

If length is given and is positive, the sequence will have so many units. If length is given and it is negative, the sequence is terminated so far from the end of the array. If this parameter is omitted, the sequence starts from offset to the end of array.

Note: array_slice () resets the array key by default. Since PHP 5.0.2, you can change this behavior by setting preserve_keys to TRUE. There is also a corresponding array_splice to remove part of the array and replace it with other values. Array array_splice (array & input, int offset [, int length [, array replacement])

Array_splice () removes the units specified by offset and length from the input array. if The replacement parameter is provided, replace it with the units in the replacement array. Returns an array containing the removed cells. Note that the number key name in input is not retained.

If the offset value is positive, it is removed from the offset specified by this value in the input array. If offset is negative, it is removed from the offset specified by the reciprocal value at the end of input.

If length is omitted, all parts from the offset to the end of the array are removed. If length is specified and it is a positive value, so many units are removed. If length is specified and it is a negative value, all units from offset to the end of the array are removed.

Tip: When replacement is given, count ($ input) is used as the length to remove all units from the offset to the end of the array. If the replacement array is provided, the removed unit is replaced by the unit in the array. If the combination of offset and length does not remove any value, the unit in the replacement array will be inserted to the position specified by offset.

Note: replacing the key names in the array is not retained. If the value to be replaced is only a unit, you do not need to add array () to it unless the unit itself is an array.

We hope that through the above introduction, you can understand the usage of the array_splice function and the method of getting the specified length data from the array.

Programmer's house. I wish you all a better learning experience.

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.