Comparing arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

Comparing arrays

Post by assgar »

Hello

I am having a problem getting the difference between two arrays.
The first array $list has all codes that can be linked to a specific id
The second array $exist has the values select and linked to a specfic id.
Both arrays are storing integers array $list should have 110,111,112,113,114,115.
Array $exist gets 110, 114 115 from the database the difference should be 111 & 112

I think the problem is with array $exist that is getting its values from the database.
Could the problem be that the arrys are storing data differently?
How do I resolve this problem?


/**==========ARRAY 1=======**/
/**All values for this type in array for comparison against**/

Code: Select all

$list = array('110','111','112','113','114','115');
/**view values in array $list**/

Code: Select all

var_dump($list);
array(6) { [0]=> string(3) "110" [1]=>  string(3) "111" [2]=>  string(3) "112" [3]=>  string(3) "113" [4]=>  string(3) "114" [5]=>  string(3) "115" } 
/**==========ARRAY 2=======**/
/**Get stored types for specific id **/

Code: Select all

 
 $exist = array();//create array 
  
  //store values in array
  $query = "SELECT type
	    FROM contact
	    WHERE id ='$id'
            AND deleted = 'N'
            ORDER BY type";
 $result = mysqli_query ($mysqli, $query);
 while($row = mysqli_fetch_array($result))
     {
        $exist[] = $row;
     }
/**View values in array for specific id in array $exist**/

Code: Select all

var_dump($exist); 
array(3){ [0]=>array(2){ [0]=>string(3) "110" ["contact_type"]=> string(3) "110"} [1]=> array(2){[0]=> string(3) "114" ["contact_type"]=> string(3) "114"} [2]=> array(2){ [0]=> string(3) "115"["contact_type"]=> string(3) "115"}} 
/**==========RESULT=======**/
/**Get the difference between all possible type and stored values linked to a specific id **/

Code: Select all

$difference = array_diff($list, $exist);
/**viewthe difference between the two arrays**/

Code: Select all

var_dump($difference);
array(6) { [0]=>  string(3) "110" [1]=>  string(3) "111" [2]=>  string(3) "112" [3]=>  string(3) "113" [4]=>  string(3) "114" [5]=>  string(3) "115" } 
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Comparing arrays

Post by superdezign »

array_diff() wrote:Returns an array containing all the entries from array1 that are not present in any of the other arrays.
Your first array is an array of strings. Your second array is an array of arrays. The result that you are getting is expected. In order for this to work, you need to either format the arrays the same way and use array_diff(), or you need to be more clear about what you are trying to achieve.
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

Re: Comparing arrays

Post by assgar »

Hello

Thanks for the suggestion.
The problem was solved here is the solution:

Code: Select all

 
 $exist = array();//create array 
  
  //store values in array
  $query = "SELECT type
	    FROM contact
	    WHERE id ='$id'
            AND deleted = 'N'
            ORDER BY type";
 $result = mysqli_query ($mysqli, $query);
 while($row = mysqli_fetch_array($result))
     {
        $exist[] = $row[0];
     }
Post Reply