我无法弄清楚foreach

时间:2016-05-11 15:41:18

标签: php foreach

我正在尝试使用php设置mxl产品Feed,但这对我来说是全新的,我希望有人可以提供帮助。我附上了我当前的代码,其中列出了具有多个sku的产品ID。

输出如下:

  

Product_ID 1
  SKU 1234
  Product_ID1
  SKU12345

我试图使用foreach

看起来像这样
  

Product_ID 1
  SKU 1234
  SKU 12345

这是我目前的代码

while ($row = $st->fetch()) {

    $xml .= "\t\t\t<Product_ID>".$row['product_id']."</Product_ID>\r\n";
    $xml .= "\t\t\t<Manufacture>".$row['manufactures_name']."</Manufacture>\r\n";
    $xml .= "\t\t\t<Product_Name>".$row['product_name']."</Product_Name>\r\n";
    $xml .= "\t\t\t<Product_Category>".$row['product_type']."</Product_Category>\r\n";
    $xml .= "\t\t\t<SKU>".$row['sku']."</SKU>\r\n";
    $xml .= "\t\t\t<UPC>".$row['upc']."</UPC>\r\n";
    $xml .= "\t\t\t<Base_Price>".$row['base_price']."</Base_Price>\r\n";
    $xml .= "\t\t\t<QtyAvailable>".$row['quantity']."</QtyAvailable>\r\n";

}

我试图在这里添加foreach,但我没有线索。

while ($row = $st->fetch()) 

    $xml .= "\t\t\t<Product_ID>".$row['product_id']."</Product_ID>\r\n";
        foreach ('product_id' == 'product_id');
           $xml .= "\t\t\t<SKU>".$row['sku']."</SKU>\r\n";

    $xml .= "\t\t\t<Manufacture>".$row['manufactures_name']."</Manufacture>\r\n";
    $xml .= "\t\t\t<Product_Name>".$row['product_name']."</Product_Name>\r\n";
    $xml .= "\t\t\t<Product_Category>".$row['product_type']."</Product_Category>\r\n";
    $xml .= "\t\t\t<UPC>".$row['upc']."</UPC>\r\n";
    $xml .= "\t\t\t<Base_Price>".$row['base_price']."</Base_Price>\r\n";
    $xml .= "\t\t\t<QtyAvailable>".$row['quantity']."</QtyAvailable>\r\n";
}

1 个答案:

答案 0 :(得分:1)

如果您正在尝试生成XML,我建议您使用PHP的SimpleXML库,因为它可以提供更大的灵活性,并且读取更清晰。有关文档,请参阅http://php.net/simplexml

文档示例:

#include <Rcpp.h>

// [[Rcpp::export]]
SEXP funx()
{
    /* creating a pointer to a vector<int> */
    std::vector<int>* v = new std::vector<int> ;
    v->push_back( 1 ) ;
    v->push_back( 2 ) ;

    /* wrap the pointer as an external pointer */
    /* this automatically protected the external pointer from R garbage 
     collection until p goes out of scope. */
    Rcpp::XPtr< std::vector<int> > p(v, true) ;

    /* return it back to R, since p goes out of scope after the return 
     the external pointer is no more protected by p, but it gets 
     protected by being on the R side */
    return( p ) ;
}