PHP 对大数组使用 for 循环时冻结

PHP freezes when using for loop for big array

我在尝试 运行 这个脚本时遇到了一些麻烦:

<?php

$mysqli=new mysqli("localhost","root","mysqlpassword","MarcTextOldBib");
if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$db_abfrage = "SELECT `cid` , `field_data` FROM T1xx" ;
$j=0;
$db_ausgabe = $mysqli->query($db_abfrage);
while($row = $db_ausgabe->fetch_object()){
    $names[$j] = array( "cid"           => $row->cid,
                        "field_data"    => $row->field_data,
                     );
    $j++;
}


$mysqli->close();

echo"
<table border='1'>
<thead><tr>
<th>ID</th> <th>Name</th>
</tr></thead>";

$ID=1;
$size = count($names);

for ( $i = 0; $i <= $size; $i++ ){
    $cid = $names[$i][cid];
    echo "<tr>";

    while( $ID != $names[$i][cid] ) {
        echo "<td>".($ID)."</td>";
        echo "<td></td>";
        echo "</tr>";
        $ID++;
    }

    if( ($ID) == $names[$i][cid] ) {
        echo "<td>" . ($ID) . "</td>";
        echo "<td>" . $names[$i][field_data] . "</td>";
        echo "</tr>";
        $ID++;
    }

}

echo "</table>";

?>

当我将 for-loop 设置为仅“50”而不是 count($names) 时,它就像魅力一样工作。但是当使用 count($names) 时,我的电脑完全死机了。 count($names) 是 2788.

将 "cid" 放在引号中。

变化:

$names[$i][cid]

收件人:

$names[$i]['cid']

也是如此
$names[$i]['fieldData']

你的代码很奇怪,我尽可能地简化了我的想法, 但是你试图用 $ID++ 得到什么我仍然无法理解。

如果你解释我会为你解决剩下的:

<?php

$mysqli=new mysqli("localhost","root","mysqlpassword","MarcTextOldBib");
if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$db_abfrage = "SELECT `cid` , `field_data` FROM T1xx" ;
$db_ausgabe = $mysqli->query($db_abfrage);
$names= array()
while($row = $db_ausgabe->fetch_assoc()){
    $names[] = $row
}


$mysqli->close();

echo"
<table border='1'>
<thead><tr>
<th>ID</th> <th>Name</th>
</tr></thead>";

$ID=1;

foreach ($names as $row ){
    $cid = $row['cid'];
    echo "<tr>";
    echo "<td>" . ($ID) . "</td>";
    echo "<td>" . ($ID != $row['cid'])?'':$row['field_data'] . "</td>";
    echo "</tr>";
    $ID++;


}

echo "</table>";

?>