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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:58:54+00:00 2026-05-29T07:58:54+00:00

I want to combine strings in PHP. My script creates every possible combination like

  • 0

I want to combine strings in PHP. My script creates every possible combination like below.

$part1 = array('','d','n','s','g');
$part2 = array('a','e','o','oo');
$part3 = array('m','n','s','d','l','t','g','j','p');
$part4 = array('g','p','l','');
$part5 = array('g','p','l');
$part6 = array('a','e','o');
$part7 = array('d','l','r','');

$names = array();

foreach ($part1 as $letter1) {
    foreach ($part2 as $letter2) {
        foreach ($part3 as $letter3) {
            foreach ($part4 as $letter4) {
                foreach ($part5 as $letter5) {
                    foreach ($part6 as $letter6) {
                        foreach ($part7 as $letter7) {
                            $names[] = $letter1 . $letter2 . $letter3 . $letter4 . $letter5 . $letter6 . $letter7;
                        }
                    }
                }
            }
        }
    }
}

But I am not happy with my solution. I is quick and dirty code. Is there a solution wich works with a flexible number of part arrays, so I can extend the script by e.g. $part8 easiely? (without changing the loop construction)

  • 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-29T07:58:55+00:00Added an answer on May 29, 2026 at 7:58 am

    Recursive one:

    function buildNames( $parts, $chars = ''){
      // Nothing to do, shouldn't happen
      if( !count( $parts)){
        return array();
      }
    
      $names = array();
      $part = array_shift( $parts);
      // Max level, we can build final names from characters
      if( !count( $parts)){
         foreach( $part as $char){
            $names[] = $chars . $char;
         }
         return $names;
      }
    
      // "We need to go deeper" and build one more level with remembered chars so far
      foreach( $part as $char){
         $names = array_merge( $names, buildNames( $parts, $chars . $char));
      }
    
      return $names;
    }
    
    $parts = array( $part1, $part2, $part3, $part4, $part5, $part6, $part7);
    $names = buildNames( $parts);
    

    From head, from scratch, comment if something, but idea should be good

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

Sidebar

Related Questions

I have PHP array of arrays as below and I want to extract the
I want to combine two array's, excluding duplicates. I am using a custom class:
I want to combine 2 parts of the same array to make a complex
Is it possible to combine Ajax post with jQuery UI dialog? And I want
I have a PHP array and want to convert it to a string. I
I want to combine the strings test/ and /go as test/go . How can
I have a report in SSRS 2008 and I want to combine some strings.
In template I have two strings which I want to combine in one. {assign
I have three arrays of strings and i want to combine them and write
Specifically, I have two lists of strings that I'd like to combine into a

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.