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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:47:02+00:00 2026-06-13T17:47:02+00:00

How can the array be separated into groups of identical elements (concatenated chars). For

  • 0

How can the array be separated into groups of identical elements (concatenated chars).
For example, I have this array:

Array(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 2
    [4] => 2
    [5] => 1
)

and want to group all identical numbers in only one element, bringing the together with concatenation to get something like this:

Array(
    [0] => 111
    [1] => 22
    [2] => 1
    )
  • 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-13T17:47:04+00:00Added an answer on June 13, 2026 at 5:47 pm

    To group all identical elements by concatenating them together (will not work for concatenating just the neighboring identical elements)

    $arr = array (1,1,1,2,2,1);
    $sum = array_count_values($arr);
    array_walk($sum, function(&$count, $value) { 
      $count = str_repeat($value, $count); 
    });
    print_r($sum);
    

    Output

    Array (
    [1] => 1111
    [2] => 22 )

    Or for concatenating just the neighboring identical elements

    $prev=null; 
    $key=0; 
    foreach ( $arr as $value ) { 
      if ($prev == $value){ 
        $res[$key] .= $value;
      } else { 
        $key++; 
        $res[$key] = $value; 
        $prev=$value; 
      } 
    }
    print_r($res);
    

    Output

    Array (
    [1] => 111
    [2] => 22
    [3] => 1 )

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

Sidebar

Related Questions

I have a comma-separated string that I want to convert into an array, so
Well I was just wondering how can I put this semicolon separated string into
I want to load tab separated input file as an array into python and
I have a multidimensinal array.. Something like this for example: Array ( [0] =>
I have a comma separated string which i explode into an array. If the
In C# and Java a byte array can be created like this byte[] b
I have an array of strings that are comma separated such as: Steve Jobs,12,CA
I have an array similar to this: list = { cat: dog, rain: sun,
I have a PHP array that I want to increment through and output the
I'm having an issue splitting a string array into a List/ArrayList. I realise this

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.