PHP - 如何将foreach数组合并到一个循环中

时间:2015-08-04 10:41:47

标签: php arrays

这是我的代码:

$url = "https://www.leaseweb.com/dedicated-servers/single-processor";

libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$n = $xpath->query('//td[@data-column-name="Model"]');
$r = $xpath->query('//td[@data-column-name="RAM"]');

foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $RAM  = $r->nodeValue;
    echo " $Name - RAM: $RAM<br>";
}

我成功回显$Name,但我无法获得$RAM的值,因为我的数组中没有它。我的问题是如何将$r添加到此foreach循环中并使其正常工作?

提前致谢!

4 个答案:

答案 0 :(得分:1)

PHP文档在这一点上有点含糊不清。 DOMXPath::query会返回一个DOMNodeList,它不是Iterator,也不是实现ArrayAccess,而是实现Traversable而不是$item = 0; foreach ($n as $entry) { $Name = $entry->nodeValue; $RAM = $r->item($item)->nodeValue; echo " $Name - RAM: $RAM<br>"; $item++; } 有一个特殊的签名,有助于遍历对象。

以下是我想到的事情:

$iterator = new IteratorIterator($r);
$iterator->rewind();
foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $node = $iterator->current();
    $RAM  = $node->nodeValue;
    $iterator->next();
    echo " $Name - RAM: $RAM<br>";
}

其他可行的方法:

using MicroMvvm;

public AuctionsViewModel : ObservableObject
{
    private string user;
    private int auction;
    private ObservableCollection<int> auctions;

    public string User
    {
        get{return user;}
        set         
        {
            if(user!=value)
            {
                user=value;
                RaisePropertyChanged("User");
            }               
        }
    }

    public int Auction
    {
        get{return auction;}
        set         
        {
            if(auction!=value)
            {
                auction=value;
                RaisePropertyChanged("Auction");
            }               
        }
    }

    public ObservableCollection<int> Auctions
    {
        get{return auctions;}
        set         
        {
            if(auctions!=value)
            {
                auctions=value;
                RaisePropertyChanged("Auctions");
            }               
        }
    }

    public ICommand Bid
    {
        get{return new RelayCommand(BidExecute);}
    }

    private void BidExecute()
    {
        Auction++;
        using (SqlConnection conn = new SqlConnection(@" Data source=ALASAD; Initial Catalog=aukcija_Arsen_Milosev; Integrated Security = true;"))
        {
            conn.Open();
            using (SqlCommand comm = new SqlCommand("UPDATE auctions SET bidValue = @bidvalue, lastBider = @lastbider",conn))
            {
                comm.Parameters.AddWithValue("@lastbider", User);
                comm.Parameters.AddWithValue("@bidValue", Auction);
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }
        Auctions.clear();           
        showData sd = new showData();
        Auctions.AddRange(sd.toString());
        RaisePropertyChanged("Auctions");
    }
}

IteratorIterator类

  

这个迭代器包装器允许将Traversable中的任何东西转换为Iterator。重要的是要理解大多数没有实现迭代器的类都有理由,因为它们很可能不允许完整的迭代器功能集。如果是这样,应提供技术以防止误用,否则会出现异常或致命错误。

答案 1 :(得分:0)

在循环中使用$ key变量

   foreach ($n as $key => $entry) {
        $Name = $entry->nodeValue;
        $RAM  = $r[$key]->nodeValue;
        echo " $Name - RAM: $RAM<br>";
    }

答案 2 :(得分:0)

您可以尝试使用当前和下一个数组函数(php doc

libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$n = $xpath->query('//td[@data-column-name="Model"]');
$r = $xpath->query('//td[@data-column-name="RAM"]');
//Reset pointer to first element and assign to currR
$currR = reset($r);

foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $RAM  = $currR->nodeValue;
    echo " $Name - RAM: $RAM<br>";
    //Move pointer to next element and assign this to currR
    $currR = next($r);
}

答案 3 :(得分:0)

$i=0;
foreach ($n as $entry) {
    $Name = $entry->nodeValue;
    $RAM  = $r[$i]->nodeValue;
    echo " $Name - RAM: $RAM<br>";
    $i++;
}