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 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块