douluhaikao93943 2014-04-03 13:50
浏览 52
已采纳

如何从XML解析中排除某些记录?

I'm pulling data from an XML file using the following code:

<?php
$url = 'http://www.inveroak.com/readerimages/livepanel/91221.xml';
$xml = simplexml_load_file($url);
$items = array();
$exclude = array('4419','4373');
$items = array_diff($items, $exclude);

foreach($xml as $Reader) { 
    $items[] = $Reader; 
}
usort ($items, function($a, $b) { 
    return strcmp($a->Status,$b->Status); 
});

foreach($items as $Reader) { 
    if($Reader->Status != 'Logged Off' && $Reader->Picture != 'None')
        {
        include '/extras/reader-single.php';
        }
}

?>

The two lines showing $exclude and $items I've added afer seeing another post excluding values from a foreach loop about excluding from XML, but when i load the page.. the two records that have the PINs specified, are still showing.

Is this the correct way to exclude pulling certain records from an XML file?

Any help is greatly appreciated!

EDIT: The four digit numbers entered are a PIN number found at Reader->PIN After thinking, could it be that it's not making the link between the number and the Reader->PIN on the xml file?

  • 写回答

3条回答 默认 最新

  • douan6815 2014-04-03 14:03
    关注

    As I told in comments, get the pin of each record, compare it to the exclude array, if it is part of the exclude array then just continue the loop. Like this:

    $url = 'http://www.inveroak.com/readerimages/livepanel/91221.xml';
    $xml = simplexml_load_file($url);
    $items = array();
    $exclude = array('4419','4373');
    $items = array_diff($items, $exclude);
    
    foreach($xml as $Reader) { 
        $items[] = $Reader; 
    }
    
    usort ($items, function($a, $b) { 
        return strcmp($a->Status,$b->Status); 
    });
    
    foreach($xml as $Reader) { 
        if($Reader->Status != 'Logged Off'
            && $Reader->Picture != 'None'
            // check if the Pin is in exclude array
            && !in_array($Reader->Pin, $exclude)
        ) { 
            include '/extras/reader-single.php';
        }   
    }
    

    Alternatively you may use array_filter():

    $url = 'http://www.inveroak.com/readerimages/livepanel/91221.xml';
    $xml = simplexml_load_file($url);
    $items = array();
    $exclude = array('4419','4373');
    $items = array_diff($items, $exclude);
    
    foreach($xml as $Reader) { 
        $items[] = $Reader; 
    }
    
    $items= array_filter($items, function($Reader) use ($exclude) {
        if($Reader->Status == 'Logged Off'
           || $Reader->Picture == 'None'
           || in_array($Reader->Pin, $exclude)
        ) { 
            return false;
        }   
        return true;
    });
    
    usort ($items, function($a, $b) {
        return strcmp($a->Status,$b->Status);
    });
    
    foreach($items as $Reader) {
        include '/extras/reader-single.php'; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?