A method of searching continuous number by comparing PHP array _php tips

Source: Internet
Author: User
Tags php programming

The example in this article describes the method of comparing PHP arrays to find consecutive numbers. Share to everyone for your reference. as follows:

$data = Array ();
$data []= Array ("01", "02", "18", "29", "31", "32");
$data []= Array ("02", "09", "11", "22", "24", "27");
$data []= Array ("07", "16", "26", "27", "29", "31");
$data []=array ("04", "05", "07", "10", "13", "25");
$data []=array ("02", "04", "05", "08", "19", "22");
$data []=array ("03", "04", "15", "25", "26", "30");
$data []=array ("01", "03", "06", "12", "16", "32");
$data []=array ("01", "05", "14", "17", "22");
  To determine more than 3 consecutive function Checkall ($sourceArr 2D) {$count = sizeof ($SOURCEARR 2D) in the table; For ($i =0 $i < $count; $i + +) {check_h ($sourceArr 2d[$i], $i);/Find horizontal direction if ($i >0) {Check_v ($SOURCEARR 2d, $i )//Looking for vertical direction check_l ($SOURCEARR 2d, $i);/Find/Direction Check_r ($SOURCEARR 2d, $i);//Find Direction}}//judge horizontal direction $h = AR
  Ray ()//Save search results on horizontal direction function check_h ($arr, $rownum) {//sort ($arr, sort_numeric);
  Global $h;
  $flag = false; For ($i =2 $i <6; $i + +) {if ($arr [$i -2]== $arr [$i]-2) {//Because the number in the array is not duplicated and ordered so it can be tricky if (! $flag) {$h [$rownUM] = Array ($arr [$i-2], $arr [$i-1], $arr [$i]);
      $flag = true;
      }else{Array_push ($h [$rownum], $arr [$i]);
  }}//judge vertical direction $v = array ();//Save search results in vertical direction function Check_v ($SOURCEARR 2d, $rownum) {global $v;
    For ($i =0 $i <6; $i + +) {$val = $sourceArr 2d[$rownum] [$i];
    if (In_array ($rownum ... $val, $v)) {continue;  } if (In_array ($val, $SOURCEARR 2d[$rownum-1]) && In_array ($val, $SOURCEARR 2d[$rownum +1])) {Array_push ($v,
      ($rownum-1) ... $val, $rownum ... $val, ($rownum + 1) ... $val);
      $tmp = $rownum + 2; while ($tmp < sizeof ($SOURCEARR 2D)) && In_array ($val, $SOURCEARR 2d[$tmp])) {Array_push ($v, $tmp ... $va
        L);
      $tmp + +; }}//decision/Direction $l = Array ()//Search results on save/direction function check_l () {}//judgment Direction $r = Array ();//Save Direction Search Result function Chec
K_r () {}//end definition, start applying checkall ($data);
echo//Horizontal Direction Search: Print_r ($h);
echo//Vertical Direction Search: Print_r ($v);
echo//Direction Search: Print_r ($l); echo//Direction Search: Print_r ($r);

 

I hope this article will help you with your PHP programming.

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.