dreamact3026 2015-08-31 04:19
浏览 41
已采纳

将XML文件与数组进行比较

I'm trying to compare to XML files. I have pulled the new.xml apart and put all the barcodes into an array. I am then trying to loop through the old.xml file and testing if the barcode exists in the array. If the barcode does exist then I want to echo a table row. When I get that working I will flip it to print if it ! exist. The XML files are structured like this:

<Products>
    <Product>
       <Id>2209</Id>
       <Barcode>4890888123702</Barcode>
    </Product>
</products>

My HTML and PHP looks like this:

<html>
<head>
    <title>Feeds = $$$$</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</head>

<body>
<div class="container-fluid">

<?php
//Get the old XML file
if (file_exists('old.xml')) {
    $oldxml = simplexml_load_file('old.xml');

    //Load all the old product IDs into an array
    $oldproducts = array();
    foreach($oldxml->Product as $oldproduct){
        $oldproducts[] = $oldproduct->Id;
    }
}else{
    echo "Old XML does not exist";
}

//Get the new xml file
if (file_exists('new.xml')){
    $newxml = simplexml_load_file('new.xml');

    //Load all the new product IDs into an array
    $newproducts = array();
    foreach ($newxml->Product as $newproduct){
        $newproducts[] = $newproduct->Id;
    }
}else{
    echo "New XML file does not exist";
}
?>
<div class="table-responsive">
    <table class="table table-striped">
        <?php
            foreach($oldxml->Product as $oldproduct) {
                $x = $oldproduct->Id;
                if(in_array($x, $newproducts)){
                    echo '<tr>';
                    echo '<td>' . $x . ' has been taken out of stock' . '</td>';
                    echo '</tr>';
                }

            }
        ?>
    </table>

</div>
</div>
</body>
</html>

The loop doesn't return anything. If I use a hard coded barcode instead of the $x, it will find it and work properly.

  • 写回答

2条回答 默认 最新

  • dongwen2896 2015-08-31 04:37
    关注

    As you are playing with SimpleXML, you may need to type cast your ID's when adding them to the arrays and performing your comparisons.

    For example, adding (int) may help here:

    foreach ($newxml->Product as $newproduct){
        $newproducts[] = (int) $newproduct->Id;
    }
    

    as well as here:

    $x = (int) $oldproduct->Id;
    

    For an attempt at further clarification to this answer, $newproduct->Id still a SimpleXMLElement when performing the comparison, at this stage it's quite possible that multiple ID elements could exist in the XML structure, when you cast it to an INT it will take the first ID element's text node and use that in the conversion to an integer.

    When you echo $newproduct->Id it performs the __tostring() conversion because the context is know. It does not know the context when used in a comparison.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)