peteykirk Posted August 7, 2013 Share Posted August 7, 2013 I am trying to figure out how to check an array for consecutive numbers and then copy the ones that arent consecutive to a new array. To make things a little more difficult the keys arent strictly numbered there are letters in front of them. Any ideas? var1 = array("PK100","PK101","PK102","PK110","PK120") should be separated into var2 = array("PK100","PK101","PK102") var3 = array("PK110","PK120") or var2 = array("PK100","PK101","PK102") var3 = array("PK110") var4 = array("PK120") Link to comment https://forums.phpfreaks.com/topic/280920-check-for-consecutive-values-in-array-and-copy/ Share on other sites More sharing options...
mac_gyver Posted August 7, 2013 Share Posted August 7, 2013 // get the numeric part function _num($val){ preg_match("/(\d)+/", $val,$d); return $d[0]; } $var1 = array("PK100","PK101","PK102","PK110","PK120"); // some data $result = array(); // where to put the results $index = 0; // sub array in the results for each consec series of data $current = _num($var1[0]); // get starting numeric part foreach($var1 as $value){ $num = _num($value); // get numeric part if($current++ == $num){ // consecutive $result[$index][] = $value; } else { // not consec $current = $num; // the new starting numeric part $index++; // the next sub array in the result $result[$index][] = $value; } } echo '<pre>'; print_r($result); Link to comment https://forums.phpfreaks.com/topic/280920-check-for-consecutive-values-in-array-and-copy/#findComment-1443837 Share on other sites More sharing options...
peteykirk Posted August 7, 2013 Author Share Posted August 7, 2013 Works like a champ!!! // get the numeric part function _num($val){ preg_match("/(\d)+/", $val,$d); return $d[0]; } $var1 = array("PK100","PK101","PK102","PK110","PK120"); // some data $result = array(); // where to put the results $index = 0; // sub array in the results for each consec series of data $current = _num($var1[0]); // get starting numeric part foreach($var1 as $value){ $num = _num($value); // get numeric part if($current++ == $num){ // consecutive $result[$index][] = $value; } else { // not consec $current = $num; // the new starting numeric part $index++; // the next sub array in the result $result[$index][] = $value; } } echo '<pre>'; print_r($result); Link to comment https://forums.phpfreaks.com/topic/280920-check-for-consecutive-values-in-array-and-copy/#findComment-1443841 Share on other sites More sharing options...
peteykirk Posted August 7, 2013 Author Share Posted August 7, 2013 Works like a champ!!! Actually I have found an issue, it groups the first set sequentially but if there are any after that it separates them into separate arrays This is my output Array( [0] => Array ( [0] => PK100 [1] => PK101 [2] => PK102 ) [1] => Array ( [0] => PK110 ) [2] => Array ( [0] => PK120 ) [3] => Array ( [0] => PK121 ) [4] => Array ( [0] => PK122 ) Should be this Array( [0] => Array ( [0] => PK100 [1] => PK101 [2] => PK102 ) [1] => Array ( [0] => PK110 ) [2] => Array ( [0] => PK120 [1] => PK121 [2] => PK122 )) Link to comment https://forums.phpfreaks.com/topic/280920-check-for-consecutive-values-in-array-and-copy/#findComment-1443866 Share on other sites More sharing options...
mac_gyver Posted August 7, 2013 Share Posted August 7, 2013 it probably has something to do with the value being assigned to $current. i'll bet if you try, you can probably find what is causing the problem fix it yourself. edit: hay, someone stuck a green badge under my avatar! Link to comment https://forums.phpfreaks.com/topic/280920-check-for-consecutive-values-in-array-and-copy/#findComment-1443869 Share on other sites More sharing options...
peteykirk Posted August 7, 2013 Author Share Posted August 7, 2013 I appreciate the help, I am currently and will continue to work on it..... Link to comment https://forums.phpfreaks.com/topic/280920-check-for-consecutive-values-in-array-and-copy/#findComment-1443872 Share on other sites More sharing options...
peteykirk Posted August 7, 2013 Author Share Posted August 7, 2013 Works like a champ!!! // get the numeric part function _num($val){ preg_match("/(\d)+/", $val,$d); return $d[0]; } $var1 = array("PK100","PK101","PK102","PK110","PK120"); // some data $result = array(); // where to put the results $index = 0; // sub array in the results for each consec series of data $current = _num($var1[0]); // get starting numeric part foreach($var1 as $value){ $num = _num($value); // get numeric part if($current++ == $num){ // consecutive $result[$index][] = $value; } else { // not consec $current = _num($value) + 1; // the new starting numeric part $index++; // the next sub array in the result $result[$index][] = $value; } } echo '<pre>'; print_r($result); Link to comment https://forums.phpfreaks.com/topic/280920-check-for-consecutive-values-in-array-and-copy/#findComment-1443878 Share on other sites More sharing options...
Psycho Posted August 7, 2013 Share Posted August 7, 2013 Here's a slightly improved version. Using RegEx is not a good idea when there are string functions that will suffice. Assuming that all the values will start with two letters then the number, substr() is a better choice. Also, there is no need to put $result[$index][] = $value; in both the if and else conditions. Just put it after the condition and remove the else entirely. My take: $inputAry = array("PK100","PK101","PK102","PK110","PK120","PK121","PK122"); // some data function parseArray($inputAry) { $outputAry = array(); $index = -1; foreach($inputAry as $value) { $num = substr($value, 2); if($num != $currentNum++) { $index++; $currentNum = $num+1; } $outputAry[$index][] = $value; } return $outputAry; } $outputAry = parseArray($inputAry); Link to comment https://forums.phpfreaks.com/topic/280920-check-for-consecutive-values-in-array-and-copy/#findComment-1443897 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.