【发布时间】:2016-11-01 04:18:58
【问题描述】:
首先,我是 php 的新手。我正在开发一个动物收容所网站(我是一名兽医/编码员),我们正在从一个可供收养的动物在线数据库中获取数据。该 xml 文件有这样的输出(这只是 xml 文件的一小部分):
<TotalWeight>64.5 lbs </TotalWeight>
<UnitWeight>lbs</UnitWeight>
<AdditionalPhotoUrls>
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55806.jpg</string>
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55807.jpg</string>
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55809.jpg</string>
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55810.jpg</string>
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55876.jpg</string>
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55877.jpg</string>
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_71558.jpg</string>
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_71559.jpg</string>
</AdditionalPhotoUrls>
<AdoptionAmount>0.0000</AdoptionAmount>
php 文件中有这段代码被转换为 json:
$item['totalweight'] = (string)$animal->TotalWeight;
$item['weight'] = (string)$animal->UnitWeight;
$item['photosX'] = (string)$animal->AdditionalPhotoUrls;
json 输出到文件,使用
echo file_put_contents('page.json', json_encode($data));
对于 TotalWeight 和 UnitWeight 是成功的,但对于 AdditionalPhotoUrls 及其字符串则不成功。 子输出是这样的(又是一小部分):
{
"weight": "lbs",
"totalweight": "65",
"photosX": "\n \n \n \n \n \n \n \n \n "
},
我不清楚我应该如何格式化:
(string)$animal->AdditionalPhotoUrls; 不获取字符串,而是以 \n
获取所有多个<string>。目前photosX的json文件为空。
任何帮助或指导将不胜感激。
【问题讨论】: