将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.

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.

Please correct you xml file its wrong formatted and change your condition with this :

if (in_array($x, (array)$newproducts[0])) {
      echo '<tr>';
      echo '<td>' . $x . ' has been taken out of stock' . '</td>';
      echo '</tr>';
}

Hope this will help you.