如何在PHP中处理非常大的数组?

时间:2017-04-21 18:34:16

标签: php

我有这段代码。

$input = 4;
$list_N = array('0', '1');
for($n=1; $n<=$input; $n++) {

    if($n%2 == 0) {
        $c++;
    }

    $reverse_list_N = array_reverse($list_N);

    $A = array();
    $B = array();

    for($i=0; $i<count($list_N); $i++) {
        $A[] = '0' . $list_N[$i];
        $B[] = '1' . $reverse_list_N[$i];
    }

    $list_N = array_merge($A[], $B[]);

    if($n == 1) {
        $list_N = array('0', '1');
    }
}
$array_sliced = array_slice($list_N, -1*$input, $input);
for($i=0; $i<count($array_sliced); $i++) {
    $output = implode("\n", $array_sliced);
}
echo "<pre>"; print_r($output); echo "</pre>";

此代码的作用是,它生成以下数据(从(0,1)开始):

0,1
00, 01, 11, 10
000, 001, 011, 010, 110, 111, 101, 100
....... and so on

输出为$input = 4;时:

1010
1011
1001
1000

正如您所看到的,在每个循环之后,$list_N数组中的元素比前一个元素加倍。如果$input = 25;采用这种速度,那么数组将具有非常大的33554432元素。这就是我无法找到解决方案的问题。当$input = 60我收到此错误时

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes)

在这一行

$list_N = array_merge($A, $B);

即使将内存限制设置为2G也无法解决问题。那么,我如何优化我的代码以使内存相同。或者,还有其他解决方案吗?

更新:以下步骤用于生成数据。

$list_N is an array
$reverse_list is the reverse of $list_N
0 is appended in the beginning of every element in the $list_N array and stored in $A
1 is appended in the beginning of every element in the $reverse_list_N array and stored in $B
Array $A and array $B are merged and is stored in $list_N.
The main loop runs for $input number of times and the last $input number of elements are displayed from the final array.

2 个答案:

答案 0 :(得分:2)

<强>解决方案

尝试使用SplFixedArray

关于:

SplFixedArray

  

大约37%的普通&#34;阵列&#34;大小相同

  

SplFixedArray类提供了数组的主要功能。优点是它允许更快的阵列实现。

来自:Documentation Page

示例:

$startMemory = memory_get_usage();
$array = new SplFixedArray(100000);
for ($i = 0; $i < 100000; ++$i) {
    $array[$i] = $i;
}
echo memory_get_usage() - $startMemory, ' bytes';

进一步阅读:

了解详情:http://nikic.github.io/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html

梅西耶解决方案:

另一个可以提供帮助的解决方案是我不推荐来覆盖默认的内存容量。你可以这样做:

ini_set('memory_limit', '-1')

进一步阅读:

http://php.net/memory_limit

答案 1 :(得分:0)

@tadman是对的。这需要一种不同的方法。我运行数字,输入65处的数组大小,假设每个数组元素只有1个字节是32 Exabytes(运行此脚本所需的内存量非常大)。

这是一个有效的解决方案(确保输入输入以使脚本运行):

//我们假设用户将输入作为有效的正整数&gt; 1

fscanf(STDIN, "%d\n", $target);

$base=["0","1"];
$root=["0","1"];

$newArr=$base;
for($i=1;$i<7;$i++)//pre-initialize root array
  $newArr=genNextArray($newArr);

//echo "-----------\n";

//print_r($root);

//we're ready to display the output now based on the root array elements
displayNBits($target);

function displayNBits($target)
{
  global $root;
  $arr=array();

  for($i=0;$i<$target;$i++)
  {
    $elem=str_pad($root[$i],$target,"0",STR_PAD_LEFT);
    $elem[0]="1";
    $arr[]=$elem;

  }

  $arr=array_reverse($arr);

  for($i=0;$i<count($arr);$i++)
    echo $arr[$i]."\n";

  //print_r($arr);
}

function genNextArray($arr)
{
  global $root;

  $newArr= array(count($arr)*2);

  $ni=0;

  //0 prefix (left to right sweep)
  for($i=0;$i<count($arr);$i++)
  {
    $newArr[$ni]="0".$arr[$i];
    $ni++;
  }

  //1 prefix (right to left sweep)
  for($i=count($arr)-1;$i>=0;$i--)
  {
    $newArr[$ni]="1".$arr[$i];
    $root[]=$newArr[$ni];
    $ni++;
  }

  return $newArr;
}