Problems with in_array

hi all

Can I have a sanity check with this code (abbreviated for clarity), I can seem to get the in_array if statements to work as intended.

The idea is that properties are imported from an XML feed, the script should make a list of properties already in the database and put them into an array, the imported properties are then checked against the array to decide whether a property should be Inserted (ie the ref of the property isnt already in the database) or Updated (ie the property ref already exists in the database)

Ive tried with both strict mode true and false, but it just dont work.



// List all properties for agency
$list = mysql_query("SELECT Prop_ID, Agents_Prop_Code
       FROM prop_t
       WHERE Agency_Code = '$agencyloggedincode'       
      ");
echo 'Number of properties found for agency - ' . mysql_num_rows($list) . '<br /><br />';
$propertyarray['test'] = 0; // make array exist only
while($row = mysql_fetch_array($list)){
 $propertyarray[$row['Prop_ID']] = $row['Agents_Prop_Code'];
}
echo 'Array of existing properties found in db : <br /><pre>' . print_r($propertyarray,true) . '</pre><br /><br />';

// get XML feed from URL
$xml_feed_url = '...............test.xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
//
$properties = produce_XML_object_tree($xml); // put data into object tree
echo '<br /><br />Fetched XML data into tree<br /><br />';

//check if good, if not faulty feed message and abort

foreach ($properties->property as $entry) { // gets each property into array
  $agents_prop_code = $entry->agents_prop_code;
  // other items removed for clarity

echo '<br /><br />Point to property ref in tree - "' . $agents_prop_code . '"<br /><br />';

// Is propref already in database

echo 'Array of properties being searched : <br /><pre>' . print_r($propertyarray,true) . '</pre><br /><br />';
 if (in_array($agents_prop_code, $propertyarray, FALSE)){ // if property already in array then we overwrite data in db
  $propid = array_search($agents_prop_code, $propertyarray); // get Prop_ID from array key
  //Update table
   echo '<br /><br />In array so Update Property ID ' . $agents_prop_code . '<br /><br />';
   $query = @mysql_query("UPDATE .............  WHERE Prop_ID = '$propid'
         ");
    
  unset($propertyarray[$propid]); // remove property from array

 }else{ // if property isnt in array then we make new entry in db
  echo '<br />Not in array so Insert Property ' . $agents_prop_code . '<br /><br />';
  $query = @mysql_query("INSERT INTO prop_t
         ");

  $propid = mysql_insert_id(); // set propid in case of new entry
 } // end of if else for property in array or not

} // end of property foreach


// here we remove all property records still in array
foreach ($propertyarray as $key => $value){
 echo '<br />Delete Property left in array ' . $key. '<br /><br />';

 $query = @mysql_query("DELETE FROM prop_t ............
      ");

}
unset($propertyarray); // then delete property array
 ?>


The results I get are

Number of properties found for agency - 1

Array of existing properties found in db :
Array(
[test] => 0
[2337] => TT02
)

Fetched XML data into tree

Point to property ref in tree - “TT01”

Array of properties being searched :
Array(
[test] => 0
[2337] => TT02
)

In array so Update Property ID TT01 **** This is not in the array but always flags as true

Point to property ref in tree - “TT02”

Array of properties being searched :
Array(
[2337] => TT02
)
In array so Update Property ID TT02 **** This time it works OK

If you are sure it is the in_array going wrong, this gives you an array you can pick up and work on and debug easily:


$xml = var_export($agents_prop_code, 1);
$db = var_export($propertyarray, 1);

echo $xml;
echo $db;

You could then show us the diffs you expect to have to then process from those two if you want.

I’m not quite sure how having an empty test key helps you, but this


$propertyarray['test'] = 0; // make array exist only

could be instantiated using


$propertyarray = array();