-1

Update:

Yes I got that part that it's not recommended to store everything in single array. I added a limit of 5000 combinations at a time but there is a new problem.

I can get first 5000 combinations but I can't think of anything to generate rest of the combinations (5001 to 10000, 10001 to 15000 and so on)

To generate first 5000 combinations, I'm using a variable that gets increased by 1 every time the loop runs and when the variable exceeds 5000, the loop is stopped and result is printed. However after 5000, if I have to generate the next 5000 combinations, the loop will have to generate first 5000 combinations too before reaching to the next 5000 combinations and this again will consume memory unnecessarily.

Hope that made sense.


I want to generate all possible combination like this:

Input:

array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),

Desired Output:

1111
1112
1113
1114
1115
1121
1122
1123
1124
1125
1131
1132
1133
1134
1135
1141
1142
1143
1144
1145
1151
1152
1153
1154
1155
1211
1212
1213
1214
1215
1221
1222
1223
1224
1225
1231
1232
1233
1234
1235
1241
1242
1243
1244
1245
1251
1252
1253
1254
1255
1311
1312
1313
1314
1315
1321
1322
1323
1324
1325
1331
1332
1333
1334
1335
1341
1342
1343
1344
1345
1351
1352
1353
1354
1355
1411
1412
1413
1414
1415
1421
1422
1423
1424
1425
1431
1432
1433
1434
1435
1441
1442
1443
1444
1445
1451
1452
1453
1454
1455
1511
1512
1513
1514
1515
1521
1522
1523
1524
1525
1531
1532
1533
1534
1535
1541
1542
1543
1544
1545
1551
1552
1553
1554
1555
2111
2112
2113
2114
2115
2121
2122
2123
2124
2125
2131
2132
2133
2134
2135
2141
2142
2143
2144
2145
2151
2152
2153
2154
2155
2211
2212
2213
2214
2215
2221
2222
2223
2224
2225
2231
2232
2233
2234
2235
2241
2242
2243
2244
2245
2251
2252
2253
2254
2255
2311
2312
2313
2314
2315
2321
2322
2323
2324
2325
2331
2332
2333
2334
2335
2341
2342
2343
2344
2345
2351
2352
2353
2354
2355
2411
2412
2413
2414
2415
2421
2422
2423
2424
2425
2431
2432
2433
2434
2435
2441
2442
2443
2444
2445
2451
2452
2453
2454
2455
2511
2512
2513
2514
2515
2521
2522
2523
2524
2525
2531
2532
2533
2534
2535
2541
2542
2543
2544
2545
2551
2552
2553
2554
2555
3111
3112
3113
3114
3115
3121
3122
3123
3124
3125
3131
3132
3133
3134
3135
3141
3142
3143
3144
3145
3151
3152
3153
3154
3155
3211
3212
3213
3214
3215
3221
3222
3223
3224
3225
3231
3232
3233
3234
3235
3241
3242
3243
3244
3245
3251
3252
3253
3254
3255
3311
3312
3313
3314
3315
3321
3322
3323
3324
3325
3331
3332
3333
3334
3335
3341
3342
3343
3344
3345
3351
3352
3353
3354
3355
3411
3412
3413
3414
3415
3421
3422
3423
3424
3425
3431
3432
3433
3434
3435
3441
3442
3443
3444
3445
3451
3452
3453
3454
3455
3511
3512
3513
3514
3515
3521
3522
3523
3524
3525
3531
3532
3533
3534
3535
3541
3542
3543
3544
3545
3551
3552
3553
3554
3555
4111
4112
4113
4114
4115
4121
4122
4123
4124
4125
4131
4132
4133
4134
4135
4141
4142
4143
4144
4145
4151
4152
4153
4154
4155
4211
4212
4213
4214
4215
4221
4222
4223
4224
4225
4231
4232
4233
4234
4235
4241
4242
4243
4244
4245
4251
4252
4253
4254
4255
4311
4312
4313
4314
4315
4321
4322
4323
4324
4325
4331
4332
4333
4334
4335
4341
4342
4343
4344
4345
4351
4352
4353
4354
4355
4411
4412
4413
4414
4415
4421
4422
4423
4424
4425
4431
4432
4433
4434
4435
4441
4442
4443
4444
4445
4451
4452
4453
4454
4455
4511
4512
4513
4514
4515
4521
4522
4523
4524
4525
4531
4532
4533
4534
4535
4541
4542
4543
4544
4545
4551
4552
4553
4554
4555
5111
5112
5113
5114
5115
5121
5122
5123
5124
5125
5131
5132
5133
5134
5135
5141
5142
5143
5144
5145
5151
5152
5153
5154
5155
5211
5212
5213
5214
5215
5221
5222
5223
5224
5225
5231
5232
5233
5234
5235
5241
5242
5243
5244
5245
5251
5252
5253
5254
5255
5311
5312
5313
5314
5315
5321
5322
5323
5324
5325
5331
5332
5333
5334
5335
5341
5342
5343
5344
5345
5351
5352
5353
5354
5355
5411
5412
5413
5414
5415
5421
5422
5423
5424
5425
5431
5432
5433
5434
5435
5441
5442
5443
5444
5445
5451
5452
5453
5454
5455
5511
5512
5513
5514
5515
5521
5522
5523
5524
5525
5531
5532
5533
5534
5535
5541
5542
5543
5544
5545
5551
5552
5553
5554
5555

I'm currently using this code to generate the combinations:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$string = '';
function combinations($arrays, $i = 0) {
    if (!isset($arrays[$i])) {
        return array();
    }
    if ($i == count($arrays) - 1) {
        return $arrays[$i];
    }

    // get combinations from subsequent arrays
    $tmp = combinations($arrays, $i + 1);

    $result = array();

    // concat each array from tmp with each element from $arrays[$i]
    foreach ($arrays[$i] as $v) {
        foreach ($tmp as $t) {
            $result[] = is_array($t) ? 
                array_merge(array($v), $t) :
                array($v, $t);
        }
    }

    return $result;
}

$array =    combinations(
        array(
           array('1','2','3','4','5'),
     array('1','2','3','4','5'),
     array('1','2','3','4','5'),
     array('1','2','3','4','5'),
        )
    );
foreach( $array as $combinations1 => $numbers )
{
     foreach( $numbers as $number )
  {
   $string .= $number;
  }
  echo $string.'<br/>'; 
  
  
  $string = ''; //unload
  
  }
?>

This code works if input is small but for larger inputs like:

array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),
array('1','2','3','4','5'),

the code throws the following error:

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 72 bytes)

Is there any better way to do what I want?

Thanks in Advance :)

newBB
  • 11
  • 1
  • Possible duplicate of [Generating all permutations of a given string](http://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string) – r3wt Feb 10 '16 at 14:32
  • Well, the error is clear. Though it doesn't depend directly from your code. It means your script is using more memory than PHP is allowed to allocate, you can either change this setting from your php.ini file or optimize your code to use less memory (as starter you don't need to save all the combinations in an array but you can print them as soon as they are generated) – valepu Feb 10 '16 at 14:33
  • Do you know how many `combinations` does your `large input` make? More than 240 Million. How do you expect to store them in the limited memory that your script gets, even if you manage to finish the loop in any reasonable amount of time – Hanky Panky Feb 10 '16 at 14:47
  • See this [code](http://codereview.stackexchange.com/questions/41510/calculate-all-possible-combinations-of-given-characters). Please note that using array with undefined (unlimited) rows, can **however** exhaust memory (also increasing memory limit) – fusion3k Feb 10 '16 at 15:27
  • @r3wt That question does not answer my query at all. I don't know how this question is a possible duplicate of that one. – newBB Feb 10 '16 at 15:53
  • 1
    your second example has 5^12=244140625 *permutations*, each one of which would need at least 12 bytes for plain text - without any overhead, without any output buffer, without anything at all. which amounts to 2.7GiB - which is a little more than the 64MiB your PHP has. so, if you want to store *all* of those permutations, you have to find more space. for example on your harddrive, pushing one line after the other, so your RAM stays free most of the time. – Franz Gleichmann Feb 10 '16 at 16:56

1 Answers1

2

why not just some nested loops?

$arr1 = array(1,2,3,4,5);
$arr2 = array(1,2,3,4,5);
...
$arrN = array(1,2,3,4,5);

$result = array();
foreach($arr1 as $val1) {
   foreach($arr2 as $val2) {
       ...
       foreach($arrN as $valN) {
           $result[] = array($val1, $val2, ..., $valN);
       }
    }
}    
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Doesn't the problem with *input* still remain there? For example OP's 12 arrays with 5 elements each will make this loop set have 244140625 iterations which is again good enough to consume a lot of memory – Hanky Panky Feb 10 '16 at 14:37
  • then OP needs to explain how the array's being used. if it's just to build a list of the all the variations, then they can be written out to a file and not stored in an array. but that's a minor problem v.s. actually building the list. – Marc B Feb 10 '16 at 14:38
  • Problem is that the OP's actual demand from this algorithm is not really sustainable for that long an input. Asking PHP to write 240 million different numbers in a file in one go will be a problem one way or another. If memory consumption gets controlled then resource and time usage will still remain a problem with that example input. It's like asking for *too much* – Hanky Panky Feb 10 '16 at 14:42
  • but only one result list would be in memory at any given time, so if the base arrays fit in there, you'd only a few extra hundred-ish bytes to store the resulting combinations. store one row, dump to disk, start another row. – Marc B Feb 10 '16 at 14:49
  • Thanks for your answer. Your code is much better than mine. Please check my updated question. – newBB Feb 10 '16 at 16:04