Home  >  Article  >  Backend Development  >  Share the method of fetching data of specified length in php array

Share the method of fetching data of specified length in php array

WBOY
WBOYOriginal
2016-07-25 08:57:571182browse
This article introduces the method of obtaining data of a specified length in an array in PHP programming. Friends in need can refer to it.

In PHP programs, 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:

<?php
$list_json = $this->accessCourse('ID DESC', 1);//查询出所以满足基本条件的数据,状态正常...
$list = Json_decode($list_json, true);//因为查询出来的是json格式这里将其转换成array格式
 
//热门课程
$toplist = array_sort($list, 'NUM', SORT_DESC);//二维数组排序
$this->assign('toplist', $toplist);
//感兴趣
$interest = findArrayList($list,9);//筛选出9条数据
$this->assign('interest', $interest);
//试听课程
$listen_yw = findArrayList($list,4,'SUBJECT',1);//语文
$listen_sx = findArrayList($list,4,'SUBJECT',2);//数学
$listen_yy = findArrayList($list,4,'SUBJECT',3);//英语
$listen_wl = findArrayList($list,4,'SUBJECT',4);//物理
$listen_hx = findArrayList($list,4,'SUBJECT',5);//化学
$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 findArrayList function.

<?php
/**
 * 查询数组
 * @param array $array
 * @param int $length
 * @param string $field
 * @param $string $value
 * @return array
 * @edit by bbs.it-home.org
 **/
function findArrayList($array, $length = null, $field = null, $value = null){
    if(!is_array($array)){
return $array;
    }
    $new_arr = array();
    if(!is_null($field)){
foreach ($array as $k=>$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);//取出数组中指定的长度
    }
    return $new_arr;
}
?>

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

If offset is non-negative, the sequence will start at this offset in array. If offset is negative, the sequence will start this far from the end in the array.

If length is given and positive, there will be this many cells in the sequence. If length is given and negative, the sequence will terminate this far from the end of the array. If omitted, the sequence will be offset from offset Start all the way to the end of array.

Note: array_slice() will reset the keys of the array by default. As of PHP 5.0.2, this behavior can be changed by setting preserve_keys to TRUE. There is also a corresponding array_splice that removes parts of an array and replaces them with other values. array array_splice ( array &input, int offset [, int length [, array replacement]] )

array_splice() removes the elements specified by offset and length in the input array. If the replacement parameter is provided, replace it with the elements in the replacement array. Returns a list containing the removed order Array of elements. Note that numeric key names in input are not preserved.

If offset is positive, removal starts from the offset specified by this value in the input array. If offset is negative, removal begins at the offset specified by this value from the end of input.

If length is omitted, all parts of the array from offset to the end are removed. If length is specified and is positive, this many cells are removed. If length is specified and is negative, all elements from offset to length counting down from the end of the array are removed.

Tips: To remove all elements from offset to the end of the array when replacement is given, use count($input) as length. If a replacement array is given, the removed cells are replaced by cells in this array. If the combination of offset and length results in no value being removed, the element in the replacement array will be inserted at the position specified by offset.

Note: Key names in the replacement array are not retained. If the value being replaced is just a cell, there is no need to add array() to it, unless the cell itself is an array.

I hope that through the above introduction, everyone can master the usage of the array_splice function and how to get data of a specified length from an array.

Programmer’s Home, I wish you all the best in your studies and progress.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn