Jump to content

foreach limitation?


brane

Recommended Posts

Hi,
  I am a bit new to php and have only written a few programs with it.  The current one I am working on will find all the permutations of a string and match them against a word list in sql , so I do not feel guilty cheating on the jumble games in the newspaper.  I have been able to find the permutations of up to six letter strings by doing:

foreach($array as $a )
  foreach($array as $b )
    foreach($array as $c )
      foreach($array as $d )
        foreach($array as $e )
          foreach($array as $f )
            $end_arr[]= $a. $b. $c. $d. $e. $f. ;

My problem is that as soon as i add another line for the seventh character in the array to be handled as $g the program will not work.  Is there a limitation as to how many nested foreach functions you can use? I am using PHP 5.0.4 on a 64 bit version of Suse 10.  Also when the program goes to compile it does not show the error and line number if it fails.  Is there a line I need to edit in the ini to turn on the debugging feature?  This feature used to help me out a lot in PHP 4 but i have since upgraded.  Also if anyone knows a better way to find the permutations of a string any help would be appreciated.  Thanks in advance
:)
Link to comment
Share on other sites

What do you mean "the program will not work?"  What is in the array "$array"?

If I understand what you correctly, you are attempting to get all the combinations of the characters in the array. For a word of 7 characters, that over 800,000 "words" and you're probably running out of time or memory or both.

Ken
Link to comment
Share on other sites

I mean that the program will read the string, split the characters into the $array,  find the size of the array, echo the size of the array, depending on the size of the array it will also echo which case it will execute as i am using a switch to diffferentiate between 5 6 and 7 letter strings, but after that for a seven letter string it will not do anything else.  It works for 5 and six letters when it prints the ending array with a line break.  I wouldn't think that the results would take up a gig of ram, but again I am pretty much a noob.  Thanks for your input :)

sorry i forgot to mention that each output will also be 7 characters long so I think that it should return 5040 results.
Link to comment
Share on other sites

[quote]so I think that it should return 5040 results.[/quote]
Your math is a little off. 5040 is 7! (factorial 7 or 7 * 6 * 5 * 4 * 3 * 2 * 1). That will only happen if you use all seven characters for one position in the word, 6 for another, and so on until there is only one left.  The way you are attempting it, you are using all seven characters in each position. so it's 7 ^ 7 or 823,543 combinations.

I would rethink your algorithm.

Ken
Link to comment
Share on other sites

try
[code]<?php
function perm($arr, $stub='') {
    global $perms;
    if (count($arr)==2) {
        $perms[] = $stub . $arr[0] . $arr[1];
        $perms[] = $stub . $arr[1] . $arr[0];
    }
    else {       
        foreach ($arr as $c) {
            $b = $arr;
            unset ($b[array_search($c, $b)]);
            perm(array_values($b), $stub.$c);
        }       
    }
}

$perms = array();

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

// view results

echo '<pre>', print_r($perms, true), '</pre>';
?>[/code]
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.