Jump to content

remove array differences only


fife

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
https://forums.phpfreaks.com/topic/287232-remove-array-differences-only/
Share on other sites

array_diff() will not work on multi-dimensional arrays. But why not get your data through an appropriate query instead of running two queries and filtering out what you don't need? I have to assume that the AssetIDs will be unique by themselves.

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.

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;} 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.