Jump to content

remove array differences only


fife
Go to solution Solved by Psycho,

Recommended Posts

I have two arrays and all I want from them is the differences to show and any item that existed in both not to show.  Please find my workings below

 

 

mysql_select_db($database_dbconnect, $dbconnect);$query_Asset = "SELECT idAsset, AssetName FROM Asset WHERE idLocation = '1'";$AssetUp = mysql_query($query_Asset, $dbconnect) or die(mysql_error()); $array1 = array();  while ($f = mysql_fetch_assoc($AssetUp)) {$array1[] = $f;}  mysql_select_db($database_dbconnect, $dbconnect);$query_DocUp = "SELECT Asset.idAsset, Asset.AssetName FROM AssetDocStand INNER JOIN Asset ON AssetDocStand.idAsset = Asset.idAssetWHERE AssetDocStand.idDocumentStandards = '2' AND Asset.idLocation ='1'";$DocUp = mysql_query($query_DocUp, $dbconnect) or die(mysql_error());$array2 = array();  while ($f = mysql_fetch_assoc($DocUp)) {$array2[] = $f;}     print_r($array1);  echo "<br><br><br>"; print_r($array2);

 
shows the following when the page is loaded.
 

Array ( [0] => Array ( [idAsset] => 10000005 [AssetName] => HP )

 [1] => Array ( [idAsset] => 10000006 [AssetName] => HP Server )

 [2] => Array ( [idAsset] => 10000009 [AssetName] => HP Laptop )

 [3] => Array ( [idAsset] => 10000010 [AssetName] => Office Printer )

[4] => Array ( [idAsset] => 10000023 [AssetName] => test ) ) 


Array ( [0] => Array ( [idAsset] => 10000023 [AssetName] => test ) )

 

 

Now when i type 

 

$array3 = array_diff($array1, $array2 ); print_r($array3);

 

I get Array ( )

 

Ive also tried array_diff_key() which also produces nothing.  All I want to be left with is

 

Array ( [0] => Array ( [idAsset] => 10000005 [AssetName] => HP )

 [1] => Array ( [idAsset] => 10000006 [AssetName] => HP Server )

 [2] => Array ( [idAsset] => 10000009 [AssetName] => HP Laptop )

 

 [3] => Array ( [idAsset] => 10000010 [AssetName] => Office Printer ))

 

 

Link to comment
Share on other sites

  • Solution

OK, after reviewing your two queries, the only query that could have records that the other would not would be the first one. The second query does an INNER JOIN to the 'Asset' table, so it would - by definition - exclude any records that don't exist in the 'Asset' table. So, this should get you the results you need.

 

 

$query = "SELECT idAsset, AssetName
          FROM Asset
          WHERE idLocation = '1'
            AND idAsset NOT IN
                (SELECT idAsset
                 FROM AssetDocStand
                 AssetDocStand.idDocumentStandards = '2') AS exclude";

 

This will get you a result of the records from the Asset table that do not have a corresponding record in the AssetDocStand with a idDocumentStandards value of 2.

Link to comment
Share on other sites

Yeah actually i just tried and fixed it with the following query

 

mysql_select_db($database_dbconnect, $dbconnect);$query_Asset = "SELECT idAsset, AssetName FROM Asset WHERE idLocation = '1' AND idAsset NOT IN (SELECT AssetDocStand.idAsset FROM AssetDocStand INNER JOIN Asset ON AssetDocStand.idAsset = Asset.idAssetWHERE AssetDocStand.idDocumentStandards = '2' AND Asset.idLocation ='1')";$AssetUp = mysql_query($query_Asset, $dbconnect) or die(mysql_error());while ($f = mysql_fetch_assoc($AssetUp)) {$array1[] = $f;} 

Link to comment
Share on other sites

You don't need the INNER JOIN in the sub-query - it only makes the query more complicated than it should be. The INNER JOIN was used for the purpose of only getting records from the AssetDocStand table where the idLocation was 1. But, the outer query will take care of that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.