【问题标题】:Foreach not showing more than one variableForeach 没有显示多个变量
【发布时间】:2012-11-12 18:22:08
【问题描述】:

目前我有以下代码,但由于某种原因它只输出一项

基本上我试图做的是说 foreach mainPropertyUrl 创建一个列表 foreach 列表但由于某种原因我只得到一个 ListingId

主提要位于 - http://api.trademe.co.nz/v1/Member/4389666/Listings/All.xml

PHP:

 $mainXmlUrl = simplexml_load_file("http://api.trademe.co.nz/v1/Member/4389666/Listings/All.xml");

 $listing = $mainXmlUrl->List->Listing;

 $mainPropertyUrl = simplexml_load_file("http://api.trademe.co.nz/v1/Listings/$listing->ListingId.xml");

在我的 HTML 中,我尝试了一个通用的 foreach 循环,但没有使用 $url 变量,因为我不确定如何使用适用于一个列表的代码来实现它。

我也尝试创建一个列表 ID 数组,但它没有显示数据:

$ListingArray = array();

            foreach ($listing[0] as $id) {

                $IDNum = $id->ListingId;

                $ListingArray[] = $IDNum;
            }

            var_dump($ListingArray);

HTML 片段:

<?php foreach ($mainPropertyUrl as $url): ?>

<div class="listingContainer">
<h2><?php echo $tradeMePropertyAddressFull;?></h2>
<div class="listingAttributes">
<div class="col0">
<span class="name"><?php echo $mainPropertyDetails[0]->Attribute[0]->DisplayName;?>:</span>
<span class="value"><?php echo $tradeMePropertyBathrooms[0];?></span>

【问题讨论】:

标签: php xml foreach


【解决方案1】:

试试:

$mainPropertyUrl = simplexml_load_file("http://api.trademe.co.nz/v1/Listings/{$listing->ListingId}.xml");

您需要在对象访问语法周围加上{}

更新:

$listing 是一个数组,所以你的循环应该是:

foreach($listing as $l) {
    $ListingArray[] = "$l->ListingId";
}

【讨论】:

  • 在没有列出其他列表的情况下,它对一个列表运行良好,{} 之前从未见过它背后的原因吗
  • 我错了,我认为只有在调用方法而不是访问属性时才需要大括号。
  • 任何想法为什么我无法获得所有详细信息?
  • 会在5后写另一个问题
【解决方案2】:

您的 $mainXmlUrl 包含一个简短格式的列表。对于这些列表中的每一个,您都需要使用新的 ListingId 调用 $mainPropertyUrl 以获取该特定列表的详细信息。

您可以遍历 $mainXmlUrl->List->Listing,并且在每个节点上,您都可以将 ListingId 作为属性检索。

$mainXmlUrl = simplexml_load_file("http://api.trademe.co.nz/v1/Member/4389666/Listings/All.xml");

$listingIds = array();

foreach ($mainXmlUrl->List->Listing as $listing) {
    // $listing->ListingId is actually an XML object that has a __toString method.
    // Hence the type casting below.
    $listingIds[] = (string) $listing->ListingId;
}

print_r($listingIds);

要检索有关每个列表的详细信息,您需要在每个列表 ID 上调用 mainPropertyUrl。

$mainXmlUrl = simplexml_load_file("http://api.trademe.co.nz/v1/Member/4389666/Listings/All.xml");

foreach ($mainXmlUrl->List->Listing as $listing) {
    $mainPropertyUrl = simplexml_load_file("http://api.trademe.co.nz/v1/Listings/{$listing->ListingId}.xml");

    // $mainPropertyUrl is an XML object containing the detailed information on one listing
    print_r($mainPropertyUrl);
}

这将导致对每个属性进行单独的 API 调用,因此如果 TradeMe 限制了您在给定时间段内可以发出的请求数量,请注意 API 速率限制。

可能有一种方法可以在一次点击中检索多个列表详情,这比点击 API 一次获取产品列表,然后再针对该结果中的每个列表再次点击要高效得多。你需要查阅他们的文档,因为我不熟悉这个 API。

$mainXmlUrl = simplexml_load_file("http://api.trademe.co.nz/v1/Member/4389666/Listings/All.xml");

foreach ($mainXmlUrl->List->Listing as $listing) {
    $mainPropertyUrl = simplexml_load_file("http://api.trademe.co.nz/v1/Listings/{$listing->ListingId}.xml");

    echo '<div class="listingContainer">';
    foreach ($mainPropertyUrl->Attributes->Attribute as $attribute) {
        echo '<span class="name">'.$attribute->DisplayName.':</span>';
        echo '<span class="value">'.$attribute->Value.'</span>';
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多