How do I generate arrangements of n elements taken k in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AccesInterzis
Forum Newbie
Posts: 4
Joined: Wed Nov 25, 2009 4:00 am

How do I generate arrangements of n elements taken k in PHP?

Post by AccesInterzis »

I have the following code:

Code: Select all

 
<?php
$string = 'abcdefghijklmnoprqstuvwxyz';
$arrangements = array();
 
$i = 0;
while ($i < strlen($string)) {
   $arrangements[] = A($i, strlen($string));//arrangements of strlen($string) elements taken $i each
   $i++;
}
?>
<pre><?php print_r($arrangements) ?></pre>
 
My problem is that I have no idea how to generate those arrangements. I have tried with substr() but unsuccessfully.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: How do I generate arrangements of n elements taken k in PHP?

Post by papa »

Do you want to put each character from a string into the array?

//loop
$arrangements[] = $string[$i]
AccesInterzis
Forum Newbie
Posts: 4
Joined: Wed Nov 25, 2009 4:00 am

Re: How do I generate arrangements of n elements taken k in PHP?

Post by AccesInterzis »

Yes. But I don`t know how to generate all the arrangements.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do I generate arrangements of n elements taken k in PHP?

Post by AbraCadaver »

papa wrote:Do you want to put each character from a string into the array?

//loop
$arrangements[] = $string[$i]

Code: Select all

$arrangements = str_split($string);
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply