Jump to content

Comparing arrays


assgar

Recommended Posts

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

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

Link to comment
Share on other sites

 

Hello

 

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

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.