PHP takes the method of specifying the length of data in the array to share

Source: Internet
Author: User
Tags php programming
This article introduces, in the PHP programming, takes the array to specify the length data the method, has the need friend reference.

In the PHP program, reduce the number of queries, but also a way to improve the overall performance of the site.

All the data that satisfies the basic criteria are queried:

 
      Accesscourse (' ID DESC ', 1);//query out so that the basic conditions of data, the state is normal ... $list = Json_decode ($list _json, true);// Because the query comes out of JSON format here to convert it to array format//Popular Course $toplist = Array_sort ($list, ' NUM ', sort_desc);//two-dimensional array sorting $this->assign (' Toplist ', $toplist);//Interest $interest = Findarraylist ($list, 9);//filter out 9 Data $this->assign (' interest ', $interest);// Audition Course $listen_yw = Findarraylist ($list, 4, ' SUBJECT ', 1);//Language $LISTEN_SX = Findarraylist ($list, 4, ' SUBJECT ', 2);//Math $ Listen_yy = Findarraylist ($list, 4, ' SUBJECT ', 3);//English $LISTEN_WL = Findarraylist ($list, 4, ' SUBJECT ', 4);//Physical $LISTEN_HX = Findarraylist ($list, 4, ' SUBJECT ', 5);//Chemical $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 for 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);//Take out the length specified in the array    }    return $new _arr;}? >

Code Description: Array_slice Remove an array array_slice (array array, int offset [, int length [, bool Preserve_keys]]) array_slice () Returns a sequence in an array of arrays specified by the offset and length parameters.

If offset is non-negative, the sequence starts at this offset in the array. If offset is negative, the sequence starts at a distance from the end of the array.

If length is given and is positive, then there will be so many cells in the sequence. If length is given and is negative, the sequence terminates so far away from the end of the array. If omitted, the sequence starts at offset from the end of the array.

Note: the Array_slice () default resets the array's keys. From PHP 5.0.2, you can change this behavior by setting Preserve_keys to TRUE. There is also a relative 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 cells specified by offset and length from the input array, and if the replacement parameter is provided, it is substituted with the cells in the replacement array. Returns an array containing the cells that have been removed. Note the numeric key names in input are not retained.

If offset is positive, the offset specified by the value in the input array begins to be removed. If offset is negative, it is removed from the offset specified by the value of the countdown at the end of input.

If length is omitted, all parts of the array from offset to end are moved. If length is specified and positive, so many cells are removed. If length is specified and negative, all cells from offset to the end of the array ending with length are removed.

Tip: When replacement is given to remove all cells from offset to the end of the array, count ($input) is used as length. If the replacement array is given, the removed cell is replaced by the cells in this array. If the combination of offset and length does not remove any values, the cells in the replacement array are inserted into the position specified by offset.

Note: The key names in the replacement array are not preserved. If the value used to replace is just a unit, you do not need to add an array () to it, unless the cell itself is an array.

We hope that through the above introduction, you can master the use of the Array_splice function, as well as the method of taking the specified length data from the array.

The programmer's home, wish everybody to study the progress.

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