Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:09:38+00:00 2026-06-14T18:09:38+00:00

I have an incredibly weird request for help here, let me try to explain

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:09:39+00:00Added an answer on June 14, 2026 at 6:09 pm
    for($i = 1; $i < count($array); $i++) {
      if( ( strlen($array[$i - 1]) - strlen($array[$i]) ) > 1) {
        $valid = false;
        break;
      }
    }
    

    Notice the start of the loop at 1 to avoid having the -1 index at $i = 0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully someone can help here, it's incredibly frustrating! I have a couple of iOS
I have an incredibly simple query (table type InnoDb) and EXPLAIN says that MySQL
Here's another part of my website, which is incredibly simple: I have the body
So lets say I have an incredibly nested iterable of lists/dictionaries. I would like
I have a script that uses Parallel::ForkManager . However, the wait_all_children() process takes incredibly
Preface: I am sure this is incredibly simple, but I have searched this site
When testing my iPhone app I have found it incredibly useful to log information
This might have been asked a million times before or might be incredibly stupid
I understand that this problem is incredibly common and I have read through quite
This should be incredibly trivial, but it's not. I have a predefined height for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.