使用关联数组
我正在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>";
i am searching for 2 less price products in soap.xml file , i used the
following code but it shows just product name and price but i want the whole detail of those two products(productname, price, quantity, availability).
can anybody help me
soap.xml file:
<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 file:
$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>";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许像这样?
Maybe like this?