fife Posted March 24, 2014 Share Posted March 24, 2014 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 )) Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 24, 2014 Share Posted March 24, 2014 (edited) 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. Edited March 24, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted March 24, 2014 Solution Share Posted March 24, 2014 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. Quote Link to comment Share on other sites More sharing options...
fife Posted March 24, 2014 Author Share Posted March 24, 2014 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;} Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 24, 2014 Share Posted March 24, 2014 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. Quote Link to comment Share on other sites More sharing options...
fife Posted March 24, 2014 Author Share Posted March 24, 2014 Thanks Psycho. Where would people like me be without you moderators. This really is the best php forum out there!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.