Title: Comparing arrays

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 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**/

$list = array('110','111','112','113','114','115');

/**view values in array $list**/

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 **/

$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**/

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 **/

$difference = array_diff($list, $exist);

/**viewthe difference between the two arrays**/

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

On line 13 of array2: $exist[] = $row; $row is an array that has TWO elements. So each time you add an element to $exist, that element does NOT have "110", "114" OR "115". It has an array, and each of those arrays in turn has TWO elements; one with index zero and one with key "contact_type". All you need to do is change: $exist[] = $row; to: $exist[] = $row[0];

Hello

Thanks for the suggestion.

The problem was solved here is the solution:

$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];
     }
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.