设置多维数组的值

时间:2011-09-12 07:08:37

标签: php mysql arrays

我目前正在向idarray []添加fileID然后再做一个foreach。但除了fileID,我还需要数组和foreach中的serverID和id,我该怎么做?所以基本上我需要找出如何向数组中再添加2个值,然后在foreach中使用这些额外的值。

$result = mysql_query("SELECT * FROM redundfiles WHERE removed=1");
while($row = mysql_fetch_array($result))
{
        $idarray[] = $row['fileID'];
}

foreach ($idarray as $value)
{
    $result = mysql_query("SELECT * FROM redundfiles WHERE fileID=$value AND removed=0 AND serverID=$XXXserverIDhereXXX");
    while($row = mysql_fetch_array($result))
    {
        $sql = "DELETE FROM redundfiles WHERE id='".$XXXidhereXXX."' ";
        mysql_query($sql) or die(mysql_error());
    }
}

1 个答案:

答案 0 :(得分:2)

为什么不将整行存储到数组中?

$rowarray[] = array();
$result = mysql_query("SELECT * FROM redundfiles WHERE removed=1");
while($row = mysql_fetch_array($result))
{
        $rowarray[] = $row;
}

foreach ($rowarray as $row)
{
    $fileid = $row['fileID'];
    $serverid = $row['serverID'];
    $result = mysql_query("SELECT * FROM redundfiles WHERE fileID=$fileid AND removed=0 AND serverID=$serverid");
    while($row = mysql_fetch_array($result))
    {
        $sql = "DELETE FROM redundfiles WHERE id='".$XXXidhereXXX."' ";
        mysql_query($sql) or die(mysql_error());
    }
}

但实际上,你的整个代码片段是一团糟。您可以使用JOIN来更有效地完成此方式。