English 中文(简体)
PHP forach loop only increment one per other iteration
原标题:PHP foreach loop only incrementing once every other iteration

这确实使我感到迷惑,因此感谢任何帮助。 我正试图产出一个成果组合的数值,但前程似乎翻了两番,从而增加了阵列指数。 因此,如果我的行文拥有1 233 45个毗连的批量,则 for的产量为11,2,2,3,3,4,4,5,5。 说明理由。 法典如下。

$result = mysql_query("SELECT * FROM Test");

echo "<table border=1><tr></tr>";

While($row = mysql_fetch_array($result))
{
    echo "<tr>";
    foreach($row as $value)
    {
        echo "<td>" . $value . "</td>";
    }
    echo "</tr><tr>";
    for($x=0;$x<5;$x++)
    {
        echo "<td>" . $row[$x] . "</td>";
    }
    echo "</tr>";
}

第二次游乐后来被扔进去,以便确保阵列实际上含有两个价值的例子。 但是,预期的这种循环产出为:1,2,3,4,5。 任何想法? 感谢。

最佳回答

mysql_fetch_array 第二个参数为<代码>。 MYSQL_FETCH_BOTH,根据该指数以及该栏的名称登记每个数值。 供应<编码>MYSQL_FETCH_NUM,仅获取数字指数:

while($row = mysql_fetch_array($result, MYSQL_FETCH_NUM)) {
 ...
问题回答

mysql_fetch_array returns an associative array using the column names of the mysql result as keys, but it also leaves them accessible by column number, so the row:

Mysql COL: A|B|C Mysql VAL: 1|2|3

will return an array that looks like this after going through the mysql_fetch_array function:

array(
    A =>1,
   0  =>1,
    B =>2
   1  =>2,
    C =>3,
   2  =>3
)

and the foreach loop will iterate through the defined and numeric keys (producing the duplication)

另一种选择是,做一个既指钥匙又指价值的 for。

foreach($row as $key=>$value)

change

    While($row = mysql_fetch_array($result))
{
    echo "<tr>";
    foreach($row as $value)
    {
        echo "<td>" . $value . "</td>";
    }
    echo "</tr><tr>";
    for($x=0;$x<5;$x++)
    {
        echo "<td>" . $row[$x] . "</td>";
    }
    echo "</tr>";
}

<><>>>

    While($row = mysql_fetch_array($result))
{
    echo "<tr>";
    foreach($row as $value)
    {
        echo "<td>" . $value . "</td>";
    }
    echo "</tr>";
}

,告诉我,你的产出是:





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签