php - Comparing XML file to an array -


i'm trying compare xml files. have pulled new.xml apart , put barcodes array. trying loop through old.xml file , testing if barcode exists in array. if barcode exist want echo table row. when working flip print if ! exist. xml files structured this:

<products>     <product>        <id>2209</id>        <barcode>4890888123702</barcode>     </product> </products> 

my html , php looks this:

<html> <head>     <title>feeds = $$$$</title>     <!-- latest compiled , 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 , 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 old xml file if (file_exists('old.xml')) {     $oldxml = simplexml_load_file('old.xml');      //load old product ids array     $oldproducts = array();     foreach($oldxml->product $oldproduct){         $oldproducts[] = $oldproduct->id;     } }else{     echo "old xml not exist"; }  //get new xml file if (file_exists('new.xml')){     $newxml = simplexml_load_file('new.xml');      //load new product ids array     $newproducts = array();     foreach ($newxml->product $newproduct){         $newproducts[] = $newproduct->id;     } }else{     echo "new xml file not exist"; } ?> <div class="table-responsive">     <table class="table table-striped">         <?php             foreach($oldxml->product $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 use hard coded barcode instead of $x, find , work properly.

as playing simplexml, may need type cast id's when adding them arrays , performing comparisons.

for example, adding (int) may here:

foreach ($newxml->product $newproduct){     $newproducts[] = (int) $newproduct->id; } 

as here:

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

for attempt @ further clarification answer, $newproduct->id still simplexmlelement when performing comparison, @ stage it's quite possible multiple id elements exist in xml structure, when cast int take first id element's text node , use in conversion integer.

when echo $newproduct->id performs __tostring() conversion because context know. not know context when used in comparison.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -