将JSON转换为XML-PHP时限制XML数据

将JSON转换为XML-PHP时限制XML数据,php,json,xml,rss,Php,Json,Xml,Rss,我正在使用这段代码将JSON转换为XML,一切正常。我在XML中获得了200条记录,但我想将其限制为50条。我只想创建包含最新50条记录的XML文件。而不是将JSON中的任何内容转换为XML function array_to_xml( $data, &$xml_data ) { foreach( $data as $key => $value ) { if( is_numeric($key) ){ $key = 'item'.$ke

我正在使用这段代码将JSON转换为XML,一切正常。我在XML中获得了200条记录,但我想将其限制为50条。我只想创建包含最新50条记录的XML文件。而不是将JSON中的任何内容转换为XML

function array_to_xml( $data, &$xml_data ) {
    foreach( $data as $key => $value ) {
        if( is_numeric($key) ){
            $key = 'item'.$key; //dealing with <0/>..<n/> issues
        }
        if( is_array($value) ) {
            $subnode = $xml_data->addChild($key);
            array_to_xml($value, $subnode);
        } else {
            $xml_data->addChild("$key",htmlspecialchars("$value"));
        }
     }
}
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
$json_file = file_get_contents("/directory/jsonfile.json");
$json_data = json_decode($json_file, true);
$json_data = array_slice((array)$json_data, 0, 50);
array_to_xml($json_data,$xml_data);
$result = $xml_data->asXML('/directory/xmlfile.xml');
函数数组到xml($data,&$xml\u数据){
foreach($key=>$value形式的数据){
如果(是数字($key)){
$key='item'.$key;//处理..问题
}
if(是_数组($value)){
$subnode=$xml\u data->addChild($key);
数组到xml($value,$subnode);
}否则{
$xml_data->addChild($key),htmlspecialchars($value));
}
}
}
$xml_data=新的SimpleXMLElement(“”);
$json_file=file_get_contents(“/directory/jsonfile.json”);
$json_data=json_decode($json_文件,true);
$json_数据=数组_切片((数组)$json_数据,0,50);
数组到xml($json\u数据,$xml\u数据);
$result=$xml_data->asXML('/directory/xmlfile.xml');
XML 这是“我的日期”节点的外观:

<dateLastModified>1493913962397</dateLastModified>
14939913962397
XML示例数据

212
0
212
123
abc-test1
111
abc
xyz
abc@xyz.ca
1.
1.
1.
222
测试名
testlastname
测试地址,
测试
城市
状态
2222
22
国家
基础知识
test@test.com
测试123
testxyz
测试信息

测试 22222 测试1 测试123 测试 rtest 123 ... ...
很大程度上取决于您对数据的控制程度。如果可以按时间戳对数据进行排序,则可以添加计数器,并在计数器达到50时退出循环

function array_to_xml( $data, &$xml_data ) {
    $count = 0;
foreach( $data as $key => $value ) {
    $count++;
    if($count == 50) {
       exit; //or return;
    }
    if( is_numeric($key) ){
        $key = 'item'.$key; //dealing with <0/>..<n/> issues
    }
    if( is_array($value) ) {
        $subnode = $xml_data->addChild($key);
        array_to_xml($value, $subnode);
    } else {
        $xml_data->addChild("$key",htmlspecialchars("$value"));
    }
 }
}
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
$json_file = file_get_contents("/directory/jsonfile.json");
$json_data = json_decode($json_file, true);
array_to_xml($json_data,$xml_data);
$result = $xml_data->asXML('/directory/xmlfile.xml');
函数数组到xml($data,&$xml\u数据){
$count=0;
foreach($key=>$value形式的数据){
$count++;
如果($count==50){
退出;//或返回;
}
如果(是数字($key)){
$key='item'.$key;//处理..问题
}
if(是_数组($value)){
$subnode=$xml\u data->addChild($key);
数组到xml($value,$subnode);
}否则{
$xml_data->addChild($key),htmlspecialchars($value));
}
}
}
$xml_data=新的SimpleXMLElement(“”);
$json_file=file_get_contents(“/directory/jsonfile.json”);
$json_data=json_decode($json_文件,true);
数组到xml($json\u数据,$xml\u数据);
$result=$xml_data->asXML('/directory/xmlfile.xml');

感谢您的快速回复。我应用了你的解决方案,但不起作用。当我回显$count时,它将呈现“12341123123445671234123445678567891011…”您可以尝试$count=$count+1;不不起作用。在将数组传递给aaray_to_xml函数之前,我还尝试将数组限制为50,但这并没有分割数据$json_data=json_decode($json_文件,true)$json_data=array_slice((array)$json_data,0,50,true);打印(数组计数值($json_数据));如果你能发布你的数据样本,那会有帮助。您可能需要在$count=$count+1周围添加一个IFstatement,其中if(key==first\u key)然后increment counter我在文章中添加了一个示例XML数据
function array_to_xml( $data, &$xml_data ) {
    $count = 0;
foreach( $data as $key => $value ) {
    $count++;
    if($count == 50) {
       exit; //or return;
    }
    if( is_numeric($key) ){
        $key = 'item'.$key; //dealing with <0/>..<n/> issues
    }
    if( is_array($value) ) {
        $subnode = $xml_data->addChild($key);
        array_to_xml($value, $subnode);
    } else {
        $xml_data->addChild("$key",htmlspecialchars("$value"));
    }
 }
}
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
$json_file = file_get_contents("/directory/jsonfile.json");
$json_data = json_decode($json_file, true);
array_to_xml($json_data,$xml_data);
$result = $xml_data->asXML('/directory/xmlfile.xml');