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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:57:27+00:00 2026-05-25T12:57:27+00:00

I have a set of strings, each string has a variable number of segments

  • 0

I have a set of strings, each string has a variable number of segments separated by pipes (|), e.g.:

$string = 'abc|b|ac';

Each segment with more than one char should be expanded into all the possible one char combinations, for 3 segments the following “algorithm” works wonderfully:

$result = array();
$string = explode('|', 'abc|b|ac');

foreach (str_split($string[0]) as $i)
{
    foreach (str_split($string[1]) as $j)
    {
        foreach (str_split($string[2]) as $k)
        {
            $result[] = implode('|', array($i, $j, $k)); // more...
        }
    }
}

print_r($result);

Output:

$result = array('a|b|a', 'a|b|c', 'b|b|a', 'b|b|c', 'c|b|a', 'c|b|c');

Obviously, for more than 3 segments the code starts to get extremely messy, since I need to add (and check) more and more inner loops. I tried coming up with a dynamic solution but I can’t figure out how to generate the correct combination for all the segments (individually and as a whole). I also looked at some combinatorics source code but I’m unable to combine the different combinations of my segments.

I appreciate if anyone can point me in the right direction.

  • 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-25T12:57:28+00:00Added an answer on May 25, 2026 at 12:57 pm

    Recursion to the rescue (you might need to tweak a bit to cover edge cases, but it works):

    function explodinator($str) {
        $segments = explode('|', $str);
        $pieces = array_map('str_split', $segments);
    
        return e_helper($pieces);
    }
    
    function e_helper($pieces) {
    
        if (count($pieces) == 1)
            return $pieces[0];
    
        $first = array_shift($pieces);
        $subs = e_helper($pieces);
    
        foreach($first as $char) {
            foreach ($subs as $sub) {
                $result[] = $char . '|' . $sub;
            }
        }
    
        return $result;
    }
    
    print_r(explodinator('abc|b|ac'));
    

    Outputs:

    Array
    (
        [0] => a|b|a
        [1] => a|b|c
        [2] => b|b|a
        [3] => b|b|c
        [4] => c|b|a
        [5] => c|b|c
    )
    

    As seen on ideone.

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

Sidebar

Related Questions

I have a txt file and each line has a set of random strings,
I have sets of strings in a database. Each set will have less than
I have a NSMutableDictionary of NSMutableSets. Each set entry is a string, something like
I have a variable, emailBody, which is set to a string stored in a
I have a table containing a large number of rows. Each row has 2
I have a table containing a large number of rows. Each row has 2
I have a set of strings with numbers embedded in them. They look something
I have a set of sql - queries: List<String> queries = ... queries[0] =
I have a SqlDependency set up using the following query: string sql = "SELECT
I have the following classes: public class Person { public String FirstName { set;

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.