Home >Backend Development >PHP Tutorial >Analysis of PHP block query implementation method

Analysis of PHP block query implementation method

不言
不言Original
2018-06-01 11:47:341449browse

This article mainly introduces the implementation method of PHP block query, and briefly analyzes the concept, principle, implementation and operation skills of PHP block query in the form of examples. Friends in need can refer to this article

The example describes the implementation method of PHP block query. Share it with everyone for your reference, the details are as follows:

Blocked query is a query method between sequential query and half query.

In fact, half query is a block query that is divided in half each time. Then block query is a query method that divides the array into blocks and then queries each block.

The array in this example is sorted and can be queried sequentially after being divided into blocks.

php code:

<?php
$arr = array(1,2,3,4,5,6,7,8,9,10);
print_r(blockSearch(3,1,$arr));
function blockSearch($block,$key,$arr){
  $length = count($arr);
  $position = 0;
  while($length >= $position){//数组元素比较完了,就结束循环
    for($i=1;$i<=$block;$i++){//循环次数为定义的块的大小
      if($arr[$position] == $key){//找到了元素
        return &#39;value:&#39;.$arr[$position] .&#39;;position:&#39;.$position;
      }
      $position++;//每比较一次,位置后移一次
    }
  }
}
?>

Run result:

value:1;position:0

Related recommendations:

PHP half (bisection) search algorithm example analysis

The above is the detailed content of Analysis of PHP block query implementation method. For more information, please follow other related articles on the PHP Chinese website!

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