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条)

报告相同问题?