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

  • SEARCH
  • Home
  • 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 6974351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:13:33+00:00 2026-05-27T17:13:33+00:00

Suppose I have the following array $diff . a, a, a, a, b, b,

  • 0

Suppose I have the following array $diff.

a, a, a, a, b, b, b, a, a, b, b, b, a, a, a, b

A represents a value inside $diff.
B represents an Array inside $diff.

Now I have to count A if it occurred more than two times in its sequence and is not an Array (instead a value). Otherwise, disregard it.

For the above input, the code should function as follows

[a] = not an array; 0
[a,a] = not an array; 0
[a,a,a] = not an array; 3
[a,a,a,a] = not an array; 4
[b] = array;
[b,b] = array;
[b,b,b] = array;
[a] = not an array; 0
[a,a] = not an array; 0
[b] = array;
[b,b] = array;
[b,b,b] = array;
[a] = not an array; 0
[a,a] = not an array; 0
[a,a,a] = not an array; 3
[b] = array;

Here is my attempt, but it doesn’t work!, the value gets changed because the values get replaced.

<?php

foreach($diff as $key => $val)  {

    if (!is_array($diff[$key])) { // THIS MEANS THAT THE CURRENT ELEMENT IS NOT AN ARRAY. 
       if(is_array($diff[$key-1]) ) {   //START OF SEQ. IF THE PREVIOUS ELEMENT IS AN ARRAY AND CURRENT ELEMENT IS NOT AN ARRAY.

        $SEQ_START=$key;
        $n=1;

            for($i=0; $i<=count($diff); $i+=1) { // I AM CHECKING HERE IF THE NEXT 3 ELEMENTS are NOT ARRAY, HENCE I CAN INCREMENT IT

            if(!is_array($diff[$SEQ_START+$i])) $n+=1;
            else $n=0;
            }
        }
    }
}

?>
  • 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-05-27T17:13:34+00:00Added an answer on May 27, 2026 at 5:13 pm

    UPDATED as per @Grexis’ comment below

    $diff = array(array(),'a', 'a', 'a', 'a', array(), array(), array(), 'a', 'a', array(), array(), array(), 'a', 'a', 'a', array());
    
    // Counter to hold current sequence total
    $count = 0;
    // Array to hold results
    $counted = array();
    
    // Loop array
    foreach ($diff as $key => $val) {
      if (is_array($val)) { // If it is an array
        if ($count > 2) { // If the counter is more than 2
          $counted[(isset($seq)) ? $seq + 1 : 0] = $count; // add it to the array
        }
        // Reset the counter
        $count = 0;
        $seq = $key;
      } else {
        // Increment the counter
        $count++;
      }
    } 
    // If there is a >2 counter at the end, add one more result
    if ($count > 2) {
      $counted[(isset($seq)) ? $seq + 1 : 0] = $count;
    }
    
    print_r($counted);
    // Outputs:
    // Array
    // (
    //     [0] => 4
    //     [12] => 3
    // )
    
    // or if you want the total count
    $total = array_sum($counted);
    echo $total; // 7
    

    See it working

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

Sidebar

Related Questions

Suppose I have the following two strings containing regular expressions. How do I coalesce
Suppose I have the following array 1.0000 2.0000 0.4231 1.0000 3.0000 0.8050 1.0000 4.0000
Suppose I have the following: typedef struct { int itemSize; int count; void *list;
How do you do SQL query for the following condition? Suppose you have two
Suppose I have the following trait with two abstract vals trait Base { val
Lets suppose i have the following array submitted by a html form: array( 'firstname'
Suppose I have an array of string like following: str_arr=[10$, 10$ I am a
I have the following code that is suppose to search an array : for
i have tested two different varianat of similary code ,suppose i have char array
Background: Suppose I have the following obviously-incorrect PHP: try{ $vtest = ''; print(array_pop($vtest)); }catch(Exception

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.