以下是我参考别人的代码改的一段求组合的代码:
<?php
$arr = range(1,25);
$t = getCombination($arr, 7);
print_r($t);function getCombination($arr,$m)
{
$result = array();
if ($m ==1)
{
foreach ($arr as $s)
{
$result[]=array($s);
}
return $result;
}
if ($m == count($arr))
{
$result[] = $arr;
return $result;
}
$temp_firstelement = $arr[0];
unset($arr[0]);
$arr = array_values($arr);
$temp_list1 = getCombination($arr, ($m-1));
foreach ($temp_list1 as $s)
{
$s[] = $temp_firstelement;
$result[] = $s;
}
unset($temp_list1); $temp_list2 = getCombination($arr, $m);
foreach ($temp_list2 as $s)
{
$result[] = $s;
}
unset($temp_list2);
return $result;
}?>由于要从46个中取7个做排列,我php.ini中memory_limit = 1500M 都没用。现在比较头疼。
大家有没什么高招?如果要让他计算完一部分将其写入数据库,该怎么改代码?另外还有个小问题,关于foreach的,如果我将foreach中as后面改为&$s,为什么内存占用反而变多了?至少我任务管理器里看到内存占用变多了。
<?php
$arr = range(1,25);
$t = getCombination($arr, 7);
print_r($t);function getCombination($arr,$m)
{
$result = array();
if ($m ==1)
{
foreach ($arr as $s)
{
$result[]=array($s);
}
return $result;
}
if ($m == count($arr))
{
$result[] = $arr;
return $result;
}
$temp_firstelement = $arr[0];
unset($arr[0]);
$arr = array_values($arr);
$temp_list1 = getCombination($arr, ($m-1));
foreach ($temp_list1 as $s)
{
$s[] = $temp_firstelement;
$result[] = $s;
}
unset($temp_list1); $temp_list2 = getCombination($arr, $m);
foreach ($temp_list2 as $s)
{
$result[] = $s;
}
unset($temp_list2);
return $result;
}?>由于要从46个中取7个做排列,我php.ini中memory_limit = 1500M 都没用。现在比较头疼。
大家有没什么高招?如果要让他计算完一部分将其写入数据库,该怎么改代码?另外还有个小问题,关于foreach的,如果我将foreach中as后面改为&$s,为什么内存占用反而变多了?至少我任务管理器里看到内存占用变多了。
$arr = range(1,5);
$t = getCombination($arr, 3);
print_r($t);function getCombination($arr,$m)
{
$result = array();
if ($m ==1)
{
foreach ($arr as $s)
{
$result[]=array($s);
}
return $result;
}
if ($m == count($arr))
{
$result[] = $arr;
return $result;
}
$temp_firstelement = $arr[0];
unset($arr[0]);
$arr = array_values($arr);
$temp_list1 = getCombination($arr, ($m-1));
foreach ($temp_list1 as $s)
{
$s[] = $temp_firstelement;
$result[] = $s;
}
unset($temp_list1); $temp_list2 = getCombination($arr, $m);
foreach ($temp_list2 as $s)
{
$result[] = $s;
}
unset($temp_list2);
return $result;
}
?>
$arr = range(1,5);
$t = getCombination($arr, 3);
print_r($t); function getCombination($arr,$m)
{
$result = array();
if ($m ==1)
{
foreach ($arr as $s)
{
$result[]=array($s);
}
return $result;
}
if ($m == count($arr))
{
$result[] = $arr;
return $result;
}
$temp_firstelement = $arr[0];
unset($arr[0]);
$arr = array_values($arr);
$temp_list1 = getCombination($arr, ($m-1));
foreach ($temp_list1 as $s)
{
$s[] = $temp_firstelement;
$result[] = $s;
}
unset($temp_list1); $temp_list2 = getCombination($arr, $m);
foreach ($temp_list2 as $s)
{
$result[] = $s;
}
unset($temp_list2);
return $result;
}
?>
第一个print_r的
Array
(
[0] => 2,3
)
Array
(
[0] => 2,3
[1] => 2,4
)
Array
(
[0] => 2,3
[1] => 2,4
[2] => 2,5
)
Array
(
[0] => 3,4
)
Array
(
[0] => 3,4
[1] => 3,5
)
Array
(
[0] => 1,2,3
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
[2] => 1,2,5
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
[2] => 1,2,5
[3] => 1,3,4
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
[2] => 1,2,5
[3] => 1,3,4
[4] => 1,3,5
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
[2] => 1,2,5
[3] => 1,3,4
[4] => 1,3,5
[5] => 1,4,5
)
Array
(
[0] => 3,4
)
Array
(
[0] => 3,4
[1] => 3,5
)
Array
(
[0] => 2,3,4
)
Array
(
[0] => 2,3,4
[1] => 2,3,5
)
Array
(
[0] => 2,3,4
[1] => 2,3,5
[2] => 2,4,5
)第二个print_r的:
Array
(
[0] => 3,4
[1] => 3,5
[2] => 4,5
)
Array
(
[0] => 2,3
[1] => 2,4
[2] => 2,5
[3] => 3,4
)
Array
(
[0] => 2,3
[1] => 2,4
[2] => 2,5
[3] => 3,4
[4] => 3,5
)
Array
(
[0] => 2,3
[1] => 2,4
[2] => 2,5
[3] => 3,4
[4] => 3,5
[5] => 4,5
)
Array
(
[0] => 3,4
[1] => 3,5
[2] => 4,5
)
Array
(
[0] => 2,3,4
[1] => 2,3,5
[2] => 2,4,5
[3] => 3,4,5
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
[2] => 1,2,5
[3] => 1,3,4
[4] => 1,3,5
[5] => 1,4,5
[6] => 2,3,4
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
[2] => 1,2,5
[3] => 1,3,4
[4] => 1,3,5
[5] => 1,4,5
[6] => 2,3,4
[7] => 2,3,5
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
[2] => 1,2,5
[3] => 1,3,4
[4] => 1,3,5
[5] => 1,4,5
[6] => 2,3,4
[7] => 2,3,5
[8] => 2,4,5
)
Array
(
[0] => 1,2,3
[1] => 1,2,4
[2] => 1,2,5
[3] => 1,3,4
[4] => 1,3,5
[5] => 1,4,5
[6] => 2,3,4
[7] => 2,3,5
[8] => 2,4,5
[9] => 3,4,5
)
Finish