使用关联数组

发布于 2024-11-07 01:25:53 字数 1930 浏览 0 评论 0原文

我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜血缘 2024-11-14 01:25:53
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>";    
 }

也许像这样?

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>";    
 }

Maybe like this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文