Php 使用$array[]的多维数组

Php 使用$array[]的多维数组,php,arrays,ubuntu,multidimensional-array,Php,Arrays,Ubuntu,Multidimensional Array,在阅读on-array push时,我发现它建议使用$array[]=$push来输入新条目 因此,我的问题是,我将如何以最有效的方式(即速度)将其用于多维数组 示例1: $client[] = (0); $client[] = (1); $client[] = (2); $client[] = (3); $array[$i++]=$client; unset($client); $array[$i++]= array(0,1,2,3); <?php $array = ar

在阅读on-array push时,我发现它建议使用$array[]=$push来输入新条目

因此,我的问题是,我将如何以最有效的方式(即速度)将其用于多维数组

示例1:

 $client[] = (0);
 $client[] = (1);
 $client[] = (2);
 $client[] = (3);
 $array[$i++]=$client;
 unset($client);
$array[$i++]= array(0,1,2,3);
<?php
$array = array();

$i=0;
$t1 = microtime(true);
while ($i<10000){
$array[$i++]= array(0,1,2,3);
}
$time1 = microtime(true) - $t1;
$mem1 = memory_get_peak_usage(true);

//print_r($array);
$array = array();
//echo '<br><br>';

$i=0;
$t2 = microtime(true);
while ($i<10000){
$client[] = (0);
$client[] = (1);
$client[] = (2);
$client[] = (3);
$array[$i++]=$client;
unset($client);
}
$time2 = microtime(true) - $t2;
$mem2 = memory_get_peak_usage(true);

//print_r($array);
$array = array();
//echo ' <br><br>';

$i=0;
$t3 = microtime(true);
while ($i++<10000){
$entry = array(0,1,2,3);
array_push($array,$entry);
}
$time3 = microtime(true) - $t3;
$mem3 = memory_get_peak_usage(true);

//print_r($array);
//echo '<br><br>';

print 'example 1 - ' . $time1 . ' - ' . $mem1 . '<br/>';
print 'example 2 - ' . $time2 . ' - ' . $mem2 . '<br/>';
print 'example 3 - ' . $time3 . ' - ' . $mem3 . '<br/>';
?>
示例2:

 $client[] = (0);
 $client[] = (1);
 $client[] = (2);
 $client[] = (3);
 $array[$i++]=$client;
 unset($client);
$array[$i++]= array(0,1,2,3);
<?php
$array = array();

$i=0;
$t1 = microtime(true);
while ($i<10000){
$array[$i++]= array(0,1,2,3);
}
$time1 = microtime(true) - $t1;
$mem1 = memory_get_peak_usage(true);

//print_r($array);
$array = array();
//echo '<br><br>';

$i=0;
$t2 = microtime(true);
while ($i<10000){
$client[] = (0);
$client[] = (1);
$client[] = (2);
$client[] = (3);
$array[$i++]=$client;
unset($client);
}
$time2 = microtime(true) - $t2;
$mem2 = memory_get_peak_usage(true);

//print_r($array);
$array = array();
//echo ' <br><br>';

$i=0;
$t3 = microtime(true);
while ($i++<10000){
$entry = array(0,1,2,3);
array_push($array,$entry);
}
$time3 = microtime(true) - $t3;
$mem3 = memory_get_peak_usage(true);

//print_r($array);
//echo '<br><br>';

print 'example 1 - ' . $time1 . ' - ' . $mem1 . '<br/>';
print 'example 2 - ' . $time2 . ' - ' . $mem2 . '<br/>';
print 'example 3 - ' . $time3 . ' - ' . $mem3 . '<br/>';
?>
示例3:注意:我目前不知道有什么好方法可以在这个示例上设置数组键

$entry = array(0,1,2,3);
array_push($array,$entry);
嵌套在数组中的4个值将非常频繁地更新。要做到这一点,我认为使用以下方法将是我在速度和效率方面的最佳选择

 $array[0][0]= $array[1][0]+1;
详细说明:我有具有唯一标识符的单个客户机。我需要为每个客户端跟踪4个整数。我正在寻找最快/使用最低资源的方法

总而言之,我会接受任何建议,但我很好奇示例1在速度和资源方面是否比示例2更好

谢谢, JT

要测试的实际代码:

 $client[] = (0);
 $client[] = (1);
 $client[] = (2);
 $client[] = (3);
 $array[$i++]=$client;
 unset($client);
$array[$i++]= array(0,1,2,3);
<?php
$array = array();

$i=0;
$t1 = microtime(true);
while ($i<10000){
$array[$i++]= array(0,1,2,3);
}
$time1 = microtime(true) - $t1;
$mem1 = memory_get_peak_usage(true);

//print_r($array);
$array = array();
//echo '<br><br>';

$i=0;
$t2 = microtime(true);
while ($i<10000){
$client[] = (0);
$client[] = (1);
$client[] = (2);
$client[] = (3);
$array[$i++]=$client;
unset($client);
}
$time2 = microtime(true) - $t2;
$mem2 = memory_get_peak_usage(true);

//print_r($array);
$array = array();
//echo ' <br><br>';

$i=0;
$t3 = microtime(true);
while ($i++<10000){
$entry = array(0,1,2,3);
array_push($array,$entry);
}
$time3 = microtime(true) - $t3;
$mem3 = memory_get_peak_usage(true);

//print_r($array);
//echo '<br><br>';

print 'example 1 - ' . $time1 . ' - ' . $mem1 . '<br/>';
print 'example 2 - ' . $time2 . ' - ' . $mem2 . '<br/>';
print 'example 3 - ' . $time3 . ' - ' . $mem3 . '<br/>';
?>

结果:

示例2-0.212869294 S

示例1-0.251849988 S

示例3-0.748561144 S

因此,阵列推送是不可能的!
这是大约15次运行的平均值,每个循环计数为100*1000:)

下面是一个快速脏测试

<?php
$t1 = microtime(true);

$array = array();

for ($i = 0; $i < 100000; $i++) {
  $client[] = (0);
  $client[] = (1);
  $client[] = (2);
  $client[] = (3);
  $array[]=$client;
  unset($client);
}

$time1 = microtime(true) - $t1;
$mem1 = memory_get_peak_usage(true);

$array = array();
$t2 = microtime(true);

for ($i = 0; $i < 100000; $i++) {
  $array[] = array(0=>array(0,1,2,3));
}

$time2 = microtime(true) - $t2;
$mem2 = memory_get_peak_usage(true);

$array = array();
$t3 = microtime(true);

for ($i = 0; $i < 100000; $i++) {
  $array[] = [0=>array(0,1,2,3)];
}

$time3 = microtime(true) - $t3;
$mem3 = memory_get_peak_usage(true);

print 'example 1 - ' . $time1 . ' - ' . $mem1 . '<br/>';
print 'example 2 - ' . $time2 . ' - ' . $mem2 . '<br/>';
print 'example 3 - ' . $time3 . ' - ' . $mem3 . '<br/>';

示例1未使用$array[1],因此结果不同。你能发布两个例子的完整代码来明确你想要比较什么吗?是的,给我几分钟@DmitriZaitsev@DmitriZaitsev代码是在我的例子中编写的,最快的是数组推送。@KeluThatsall使用您尝试的内容添加一个答案当我查看示例时,我注意到一个错误。这个测试的问题是,一个数组的大小在增长,而另一个数组的大小在过度编写。我更新了答案底部的代码。你提出了一个优点,我为自己感到羞愧。我改变了主意,你重新运行测试了吗?我做了,我发现第一个示例的时间效率最低,但内存效率最高,示例3在时间上略高于示例2,但在内存上是相等的。我已经有了,只是增加了[]到$array以防止重写,以及在每次测试之前重置$array的两个$array=array()