我在soap.xml文件中搜索2个价格较低的产品,我使用了 下面的代码,但它只显示产品名称和价格,但我想要这两个产品的全部细节(产品名称,价格,数量,可用性)。 任何人都可以帮助我
soap.xml文件:
<products>
<product>
<proname>Lux soap</proname>
<quantity>5</quantity>
<price>10</price>
<available>yes<available>
</product>
<product>
<proname>Pamela Soap</proname>
<quantity>5</quantity>
<price>5</price>
<available>yes<available>
</product>
<product>
<proname>Camery Soap</proname>
<quantity>5</quantity>
<price>15</price>
<available>yes<available>
</product>
</products>
php文件:
$doc= DOMDocument::load("soaps.xml");
$product = $doc->getElementsByTagName("product");
echo "<table><tr><th>Product_name</th><th>Quantity</th><th>Price</th><th>Available</th></tr>";
$prod=array();
foreach($product as $node)
{
$proname = $node->getElementsByTagName("proname");
$proname = $proname->item(0)->nodeValue;
$quantity = $node->getElementsByTagName("quantity");
$quantity = $quantity->item(0)->nodeValue;
$price = $node->getElementsByTagName("price");
$price = $price->item(0)->nodeValue;
$availble = $node->getElementsByTagName("available");
$available= $available->item(0)->nodeValue;
$prod[$price]=$proname;
}
ksort($prod, SORT_NUMERIC);
$a = 0;
foreach($prod AS $pri => $nam)
{
echo"<tr><td>{$nam}</td><td>{$pri}</td></tr>";
$i++;
if( $i == 2)
break;
}
echo "</table>";
答案 0 :(得分:0)
foreach($product as $node)
{
$proname = $node->getElementsByTagName("proname");
$proname = $proname->item(0)->nodeValue;
$quantity = $node->getElementsByTagName("quantity");
$quantity = $quantity->item(0)->nodeValue;
$price = $node->getElementsByTagName("price");
$price = $price->item(0)->nodeValue;
$availble = $node->getElementsByTagName("available");
$availble = $available->item(0)->nodeValue;
$prod[$price]= array($proname, $quantity, $price, $availble);
}
ksort($prod, SORT_NUMERIC);
$prod = array_slice($prod,0,2);
foreach($prod AS $pri => $nam)
{
echo"<tr><td>{$nam[0]}</td><td>{$nam[1]}</td><td>{$nam[2]}</td><td>{$pri}</td></tr>";
}
也许是这样的?