I have an incredibly weird request for help here, let me try to explain it as best as possible:
I have an array of items (I've hashed the text as the text isn't important, it's the length I'll be using)
Array
(
[0] => ##
[1] => ###
[2] => ###
[3] => ###
[4] => ####
[5] => ###
[6] => ####
[7] => #####
[8] => ##
[9] => ###
)
What I need to check is to make sure that the lengths of each value is not more than 1 greater than the length of the previous element. They can decrease in length more than 1, but they can't increase in length more than 1. The above array is valid according to these rules.
The array below, however, would be erroneous (see how element 4's length is 2 greater than 3).
Array
(
[0] => ##
[1] => ###
[2] => ###
[3] => ###
[4] => #####
[5] => ###
[6] => ####
[7] => #####
[8] => ##
[9] => ###
)
Here is what I have so far:
<?php
$array = array('##','###','###','###','####','###','####','#####','##','###'); // Valid Array
$array = array('##','###','###','###','#####','###','####','#####','##','###'); // Invalid Array
$tmp = 0;
$i = 0;
$valid = true;
foreach($array as $k => $v) {
if($i>0&&strlen($v)>$tmp&&strlen($v)>($tmp+1)) {
$valid = false;
}
$tmp = strlen($v);
$i++;
}
echo ($valid) ? 'Array is valid' : 'Array is invalid';
?>
It works, but it's messy as hell and I'm not happy with it at all, anyone have any other suggestions on a better way to check for this?