Php 我的json对象只输出一个对象,但xml文件中还有更多的对象
这是我的JSON输出,我会喜欢更多的对象Php 我的json对象只输出一个对象,但xml文件中还有更多的对象,php,json,xml,Php,Json,Xml,这是我的JSON输出,我会喜欢更多的对象 [ { "EcommerceProductGuid": "1282174A-F6A9-4049-8C03-EFDBF065A22F", "ProductNumber": "-004394", "Description": "Sparta Ion DLI D50", "Type": "Ion DLI M-Gear", "Kind": "Elektrische fiets
[
{
"EcommerceProductGuid": "1282174A-F6A9-4049-8C03-EFDBF065A22F",
"ProductNumber": "-004394",
"Description": "Sparta Ion DLI D50",
"Type": "Ion DLI M-Gear",
"Kind": "Elektrische fiets",
"Brand": "Sparta",
}
]
这是我的PHP
function XMLtoJSON($xml) {
$xml = file_get_contents($xml); // gets XML content from file
$xml = str_replace(array("\n", "\r", "\t"), '', $xml); // removes newlines, returns and tabs
// replace double quotes with single quotes, to ensure the simple XML function can parse the XML
$xml = trim(str_replace('"', "'", $xml));
$simpleXml = simplexml_load_string($xml);
// loop over Products -> Product item in the xml file
$devices = array();
foreach($simpleXml->Products->Product as $product)
{
$device = array();
foreach($product as $key => $value)
{
//$device[(string)$rewriteKeys[$key]] = (string)$value;
$device[(string)$key] = (string)$value;
// unset empty and extra keys
unset($device['epg']);
unset($device[null]);
}
$devices[] = $product;
return stripslashes(json_encode($devices, JSON_PRETTY_PRINT)); // returns a string with JSON object
}
问题是,他只输出一个产品对象,但我的XML文件包含其中的4个对象。在脚本末尾有一些问题,您需要将设备添加到主循环内的数组中,但在所有循环结束后输出JSON(您可以在第一个循环结束时返回数据)
您缺少第一个
foreach
的结束}
。因此,返回总是在第一个foreach
的第一个循环中执行:
function XMLtoJSON($xml) {
$xml = file_get_contents($xml); // gets XML content from file
$xml = str_replace(array("\n", "\r", "\t"), '', $xml); // removes newlines, returns and tabs
// replace double quotes with single quotes, to ensure the simple XML function can parse the XML
$xml = trim(str_replace('"', "'", $xml));
$simpleXml = simplexml_load_string($xml);
// loop over Products -> Product item in the xml file
$devices = array();
foreach($simpleXml->Products->Product as $product)
{
$device = array();
foreach($product as $key => $value)
{
//$device[(string)$rewriteKeys[$key]] = (string)$value;
$device[(string)$key] = (string)$value;
// unset empty and extra keys
unset($device['epg']);
unset($device[null]);
}
$devices[] = $product;
} // THIS WAS MISSING
return stripslashes(json_encode($devices, JSON_PRETTY_PRINT)); // returns a string with JSON object
}
在for循环外部返回json数据,然后查看结果您的意思是在函数外部吗?但我认为他已经在循环之外了,对吗?不是在函数之外,循环的终点在哪里
function XMLtoJSON($xml) {
$xml = file_get_contents($xml); // gets XML content from file
$xml = str_replace(array("\n", "\r", "\t"), '', $xml); // removes newlines, returns and tabs
// replace double quotes with single quotes, to ensure the simple XML function can parse the XML
$xml = trim(str_replace('"', "'", $xml));
$simpleXml = simplexml_load_string($xml);
// loop over Products -> Product item in the xml file
$devices = array();
foreach($simpleXml->Products->Product as $product)
{
$device = array();
foreach($product as $key => $value)
{
//$device[(string)$rewriteKeys[$key]] = (string)$value;
$device[(string)$key] = (string)$value;
// unset empty and extra keys
unset($device['epg']);
unset($device[null]);
}
$devices[] = $product;
} // THIS WAS MISSING
return stripslashes(json_encode($devices, JSON_PRETTY_PRINT)); // returns a string with JSON object
}