Php array comparison to find the continuous number, php array Query
This example describes how to compare php arrays to find continuous numbers. Share it with you for your reference. The details are 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 "); // identifies more than three consecutive functions in the table checkAll ($ sourceArr2D) {$ count = sizeof ($ sourceArr2D); for ($ I = 0; $ I <$ count; $ I ++) {check_h ($ sourceArr2D [$ I], $ I); // find the horizontal direction if ($ I> 0) {check_v ($ sourceArr2D, $ I); // find the vertical direction check_l ($ sourceArr2D, $ I ); // find/Direction check_r ($ sourceArr2D, $ I); // find direction }}// judge the horizontal direction $ h = array (); // Save the search result in the 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) {// if (! $ Flag) {$ h [$ rownum] = array ($ arr [$ I-2], $ arr [$ I-1], $ arr [$ I]); $ flag = true;} else {array_push ($ h [$ rownum], $ arr [$ I]) ;}}} // determine the vertical direction $ v = array (); // Save the search result function check_v ($ sourceArr2D, $ rownum) {global $ v; for ($ I = 0; $ I <6; $ I ++) {$ val = $ sourceArr2D [$ rownum] [$ I]; if (in_array ($ rownum... $ val, $ v) {continue;} if (in_array ($ val, $ sourceArr2D [$ rownum-1]) & in_array ($ val, $ sourceArr2D [$ rownum + 1]) {array_push ($ v, ($ rownum-1 )... $ val, $ rownum... $ val, ($ rownum + 1 )... $ val); $ tmp = $ rownum + 2; while ($ tmp <sizeof ($ sourceArr2D) & in_array ($ val, $ sourceArr2D [$ tmp]) {array_push ($ v, $ tmp... $ val); $ tmp ++ ;}}/// judge/Direction $ l = array (); // save/search result function check_l () in the direction () {}// judgment direction $ r = array (); // Save the search result function check_r () {}// end the definition and start to apply checkAll ($ data ); echo // horizontal direction: print_r ($ h); echo // vertical direction: print_r ($ v); echo // direction: print_r ($ l); echo // search in the direction: print_r ($ r );
I hope this article will help you with php programming.