坐标2 foreach循环

时间:2012-07-12 16:44:35

标签: php foreach

我有两个foreach循环,它们都循环通过相同的数组;然而,在使用str_replace之后循环遍历数组,而另一个不使用str_replace;

我的问题;我如何协调这两个循环,以便它们同时循环通过同一个数组?

抱歉,如果我的问题没有描述性;

$contents = ftp_nlist($conn_id, $dir);
$unalteredcontents = ftp_nlist($conn_id, $dir);
foreach ($unalteredcontents as $fname2)
{
    if (($fname2 != '.') && ($fname2 != '..'))
    {
        $url = "http://mywebsite.com/folder/" . $userid . "/" . $fname2 . "";
        next($fname2);
    }
}
$contents = str_replace("-", '', $contents);
$contents = str_replace(".pdf", '', $contents);

foreach ($contents as $fname)
{
    if (($fname != '.') && ($fname != '..'))
    {
        $sql_select = "INSERT INTO `database`.`column` (`data`, `fileurl`) VALUES ('" . $fname . "', '" . $url . "') ";
    }
}

2 个答案:

答案 0 :(得分:1)

你的问题不清楚。但是我试一试:

$unalteredcontents = ftp_nlist($conn_id, $dir);
foreach ($unalteredcontents as $fname2)
{
    if (($fname2 != '.') && ($fname2 != '..'))
    {
        $url = "http://mywebsite.com/folder/" . $userid . "/" . $fname2 . "";

        $contents = str_replace("-", '', $fname2);
        $contents = str_replace(".pdf", '', $contents);

        $sql_select = "INSERT INTO `database`.`column` (`data`, `fileurl`) VALUES ('" . $contents. "', '" . $url . "') ";
    }
}

答案 1 :(得分:1)

这样的事情:

$unalteredcontents = ftp_nlist($conn_id, $dir);
$contents = str_replace("-", '', $unalteredcontents);
$contents = str_replace(".pdf", '', $contents);

foreach ($contents as $key => $fname)
{
    if (($fname != '.') && ($fname != '..'))
    {
        $url = "http://mywebsite.com/folder/" . $userid . "/" . $unalteredcontents[$key];
        $sql_select = "INSERT INTO `database`.`column` (`data`, `fileurl`) VALUES ('" . $fname . "', '" . $url . "') ";
    }
}