HI Everyone,

Having a bit of trouble with some array`s...

here is the situation:

I need to get the contents of 1 array and compare them to another array and if numbers from the 2nd array are in the first array, i want to unset it from the 2nd array.

so basically,

$arr1 = array('1','2','3','4','5','6','7','158','12');
$arr2 = array('2','5','4','8','12','6','7','154','124','131');

$arr1 comes from the table with figures in already, the line for line,
$arr2 comes from tickboxes and is the 1 i will use to update both tables,

there is a catch, I am updating 2 tables, 1 table accepts and array of figures while the other takes line for line input.
I need to update both.

So the 2nd array needs to be unchanged and update table 2 which accepts the string of figures and then i need to change it for the line for line update.

something i did before but i forgot to compare array`s so data gets duplicated in INSERT INTO.

foreach ($com as $i){
	$sql_insert = "INSERT INTO `table1` (ID, CompID) VALUES ('{$id}', '{$i}')";
	$rs_insert = dbconn($sql_insert);
        }

foreach ($com as $t){
     $up .=  $t.",";
   }
   $output = substr($up, 0, -1);
   $sql_upd = "UPDATE `table2` SET Computer = '".$output."' WHERE ID = '".$id."' ";
   $rs_upd = dbconn($sql_upd);
   
   echo "Updated.... Thank You!";
  <script>location.href='example.php?ID=".$id."';</script>";
  }

i know it is something simple... but any help is appreciated! :)

Recommended Answers

All 2 Replies

<?php 		
$arr1 = array('1','2','3','4','5','6','7','158','12');
$arr2 = array('2','5','4','8','12','6','7','154','124','131');

echo "<pre>";

$insert =array_diff($arr2,$arr1);
$update =array_intersect($arr2,$arr1);
//print_r($update);

foreach($update as $key)
{
	echo $key." update <br>";
}
echo "<br><br>";
foreach($insert as $key)
{
	echo $key." insert<br>";
}
echo "</pre>";
?>

Thanks urtrivedi, worked like a charm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.