使用PHP forEach和SimpleXML从API创建XML文件
我试图从API(在本例中是Instagram API)创建XML文件。 但我面临一个问题,因为我用forEach函数创建它。从产品部分查看productName和productPrice属性:使用PHP forEach和SimpleXML从API创建XML文件,php,xml,Php,Xml,我试图从API(在本例中是Instagram API)创建XML文件。 但我面临一个问题,因为我用forEach函数创建它。从产品部分查看productName和productPrice属性: <product productName="No Title" productID="001" thumbPath="http://IMAGE_URL" productPrice="0"> <details> detail1 </details
<product productName="No Title" productID="001" thumbPath="http://IMAGE_URL" productPrice="0">
<details>
detail1
</details>
</product>
<product productName="Sushi Homemade " productID="002" thumbPath="http://IMAGE_URL" productPrice="50.000 ">
<details>
detail2
</details>
</product>
<product productName="Sushi Homemade " productID="003" thumbPath="http://IMAGE_URL" productPrice="50.000 ">
<details>
detail3
</details>
</product>
详情1
细节2
细节3
Value productName=“Sushi自制”和productPrice=“50.000”仅适用于productID=“002”。但为什么该值也填充到下一个productID>=002
下面是使用PHP创建xml的代码片段。也许我会弄错的:
<products>
<category categoryName="Instagram">
<?php foreach ($contents->data as $data) {
foreach($data->comments->data as $comment){
if(preg_match('/#title/', $comment->text)){
$komen = preg_replace('/#title/', '', $comment->text);
break;
} else { $komen = 'No Title'; }
}
foreach($data->comments->data as $comment){
if(preg_match('/#price/', $comment->text)){
$harga = preg_replace('/#price/', '', $comment->text);
break;
} else { $harga = '0'; }
}
echo '<product productName="'. $komen .'" productID="' . $data->id . '" thumbPath="' . $data->images->thumbnail->url . '" productPrice="'. $harga .'">
<details>
<![CDATA[
<img src="' . $data->images->low_resolution->url . '" width="100%"/>
' . $data->caption->text . '
]]>
</details>
</product>
你的问题是什么,什么不起作用?这也可能重复。不,这是另一个新问题。查看XML结果:(我不能在这里粘贴格式化的XML)。问题是:Value productName=“Sushi自制”和productPrice=“50.000”仅适用于productID=“002”。但为什么该值也填充到下一个productID>=002。