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 7861127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:34:25+00:00 2026-06-02T22:34:25+00:00

major headache here, I’m tiring to iterate over both keys and values and manipulate

  • 0

major headache here, I’m tiring to iterate over both keys and values and manipulate the pointer to make a comparison on the difference between the integer in the array-keys. I’m trying to split and arrange this array based on subtraction of the array-keys being greater than 1, I noticed this early on as a valid method to separate the values into clusters as explained at the bottom of this question.

eg:([0] => cat[2] => fruit [3] => apple) keys equate to:

2nd – 1st keys: 2-0 = 2 (2>1)

3rd – 2nd keys: 3-2 = 1 (1=1)

I’ve given a concise explanation of what I’m trying to accomplish at the bottom of the question.

array code below:

$arr1 = array("cat", "Van", "fruit", "apple", "orange", "banana", "car", "car", "boat", "Bike", "truck", "cat", "dog", "bus", "van", "truck", "Sea", "ocean", "sea", "ship", "train", "Land");
$arr2 = array("cat", "fruit", "apple", "orange", "banana", "cat", "dog", "sea", "ocean", "land");
$array = array_uintersect($arr1, $arr2, 'strcasecmp');

//returns:

Array
(
    [0] => cat
    [2] => fruit
    [3] => apple
    [4] => orange
    [5] => banana
    [11] => cat
    [12] => dog
    [16] => Sea
    [17] => ocean
    [18] => sea
    [21] => Land
)

(below) is code I’ve so far written as my attempted implementation to separate as explained at the bottom of this question:

$arr1 = array("cat", "Van", "fruit", "apple", "orange", "banana", "car", "car", "boat", "Bike", "truck", "cat", "dog", "bus", "van", "truck", "Sea", "ocean", "sea", "ship", "train", "Land");
$arr2 = array("cat", "fruit", "apple", "orange", "banana", "cat", "dog", "sea", "ocean", "land");
foreach($array as &$val){
$array2 = $array;
prev($array);
if(key($array2)-key($array)!==1) { //bool to determine if the difference in keys is only 1 integer apart. 
$val = array($val);
$val[] = "";
}}
unset($val);
print_r(collapse_multi_arrays($array)); //collapse_multi_arrays is a custom function I have to form a single array from multidimensional ones. 

outputs:
Array
(
    [0] => cat
    [1] => 
    [2] => fruit
    [3] => apple
    [4] => orange
    [5] => banana
    [6] => 
    [7] => cat
    [8] => dog
    [9] => 
    [10] => Sea
    [11] => ocean
    [12] => sea
    [13] => 
    [14] => Land
    [15] => 
)

(output above) not sure if it’s the wisest way to iterate over the array but I’m wanting to use the empty values to chunk or split somehow in order to get something like this:

Array
(
    [0] => Array
        (
            [0] => cat
        )

    [1] => Array
        (
            [2] => fruit
            [3] => apple
            [4] => orange
            [5] => banana
        )

    [2] => Array
        (
            [11] => cat
            [12] => dog
        )

    [3] => Array
        (
            [16] => Sea
            [17] => ocean
            [18] => sea
        )

    [4] => Array
        (
            [21] => Land
        )

)

which I think I can append to each cluster to one value each
like this:

Array
(
    [0] => cat
    [1] => fruit apple orange banana
    [2] => cat dog
    [3] => Sea ocean sea
    [4] => Land

)

//========================================================

overall explanation:
what I’m trying to do overall is something like,

from this:

Array
(
    [0] => cat
    [2] => fruit
    [3] => apple
    [4] => orange
    [5] => banana
    [11] => cat
    [12] => dog
    [16] => Sea
    [17] => ocean
    [18] => sea
    [21] => Land
)

to this:

Array
(
    [0] => cat
    [1] => fruit apple orange banana
    [2] => cat dog
    [3] => Sea ocean sea
    [4] => Land

)

which is to cluster-append array values together when the difference in array-keys are only 1 integer apart.
if the difference in array-key integers is greater than 1 then cluster-appended array values are passed to next array-key,
as in the from this *to this* key-values above example

Any help, advice, code would be most grateful as I’m finding it difficult to iterate over both keys and values to achieve this goal, cheers.

  • 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-02T22:34:30+00:00Added an answer on June 2, 2026 at 10:34 pm

    What jumps to my mind is a simple solution like the following (tested and producing the wanted result):

    $a = array(
        0 => 'cat',
        2 => 'fruit',
        3 => 'apple',
        4 => 'orange',
        5 => 'banana',
        11 => 'cat',
        12 => 'dog',
        16 => 'Sea',
        17 => 'ocean',
        18 => 'sea',
        21 => 'Land',
    );
    
    $last = 0;
    $stack = $grouped = array();
    foreach ($a as $key => $val) {
        if ($key - $last > 1) {
            $grouped[] = implode(' ', $stack);
            $stack = array();
        }
        $stack[] = $val;
        $last = $key;
    }
    $grouped[] = implode(' ', $stack);
    print_r($grouped);
    

    I’m not sure what you are trying to achieve here though as it looks kind of strange 😉 Whatever is the underlying problem your trying to solve, I’d think theres a more elegant solution than this.

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

Sidebar

Related Questions

The major difference between a thing that might go wrong and a thing that
That’s a pretty major difference. This is why it’s always strongly recommended that you
I'm having some major headache trying to apply CSS3 transitions to a slideshow trough
What is the major difference between $(window).width() vs $(document).width() in jQuery? Whether window denotes
I have a major headache trying to get an image that is contained within
I have a table (Major) without any foreign keys and just two fields: ID
I'm writing a scanner as part of a compiler. I'm having a major headache
Major bash-noob here. Writing my second program again. For my homework, I need to
Major Edit: I misread the article! The comment was in regards to the finalize
Some major JVM classes (such as String or List implementations ) implement equals by

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.