Jump to content

Creating all possible combinations from array.


Chris92

Recommended Posts

Hi. I need some help working something out.

 

I have an array that has all the numbers from, 0 to 9. What I want to do is loop through the array to make all the combinations possible up to a string thats 255 chars long.

 

$array = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' );

 

Should be looped through to create strings like this:

0
1
2
3
4
5
6
7
8
9
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23 etc..

 

Just as if it's counting.

Link to comment
Share on other sites

This would do it, but I really don't think you have any idea how big the resultant set of numbers actually is.

<?php
for( $i = 0; $i < 1E256; $i++) {
echo "$i<br>";
}
?>

[/code]

No.. you're adding numbers.. maybe i should have made an example with letters.

 

Say I were to do:

$array = array( 'a', 'b', 'c', 'd', 'e', 'f' );

 

I want to make it loop through the array and get a result like this:

a
b
c
d
e
f
aa
ab
ac
ad
ae
af
ba
bb
bc
bd
be
bf
ca
cb
cc
cd
ce
cf
da
db
dc
dd
de
df
ea
eb
ec
ed
ee
ef
fa
fb
fc
fd
fe
ff
aaa
aab
aac
aad
aae
aaf
aba
abb
abc... etc

 

And yes I realise how big this would be.. I'm not planning to do it all in one go.

Link to comment
Share on other sites

A few billion? It should be 1.0x10256, or this many

1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

 

But anyhow, Let's see what we can come up with. This is strictly proof-of-concept code here. It doesn't put them in the order you wanted, but it does spit out some numbers. It would need to be modified to use anything other than numbers, or take an array as input. I haven't done anything other than see if it would run from 0-9, so I don't know what other issues may surface.

 

<?php
$max = 10; // to get full range of values, change to $max = 10E256;
for( $n = 0; $n < $max; $n++ ) {
   $str = $n;
   for( $i = 1; $i < 256; $i ++ ) {
      echo str_pad($str, $i, '0', STR_PAD_LEFT) . '<br>';
   }
}
?>

Link to comment
Share on other sites

A few billion? It should be 1.0x10256, or this many

1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

 

But anyhow, Let's see what we can come up with. This is strictly proof-of-concept code here. It doesn't put them in the order you wanted, but it does spit out some numbers. It would need to be modified to use anything other than numbers, or take an array as input. I haven't done anything other than see if it would run from 0-9, so I don't know what other issues may surface.

 

<?php
$max = 10; // to get full range of values, change to $max = 10E256;
for( $n = 0; $n < $max; $n++ ) {
   $str = $n;
   for( $i = 1; $i < 256; $i ++ ) {
      echo str_pad($str, $i, '0', STR_PAD_LEFT) . '<br>';
   }
}
?>

 

Okay even beter.. the more possibilities the better.

 

Again you're just adding a few numbers together in your loop. That's not the point. It's supposed to loop through the array. Once it's reached the length of the array, loop through it again + with another loop. Then once it's reached the length of the array^2. Loop through it again + another loop + another loop, until it reaches the length of the array^3. etc etc etc. Right up to the length of the array^255.

Link to comment
Share on other sites

I didn't say it was a finished, plug-and-play solution, did I?

 

Oh sorry.

 

I've come up with this:

<?php

header('content-type: text/plain');

$array = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$level = 0;
foreach( $array as $key => $value )
{

if( $key+1 == count($array) )
{
	$level++;
}

if( $level == 0 )
{
	echo $value ."\n";
}
else
{
	while( $level != count($array) )
	{
		foreach( $array as $key => $value )
		{
			echo $array[$level] . $value ."\n";
		}
		$level++;
	}
}
}

?>

 

I can't figure out how to get it to make it's own loops depending on the level.

 

Any ideas?

 

Thanks for the help BTW.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.