Jump to content

Check for consecutive values in array and copy


Go to solution Solved by mac_gyver,

Recommended Posts

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")

  • Solution

// 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);

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);

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        ))

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!

Edited by mac_gyver

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);

 

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);
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.