Solved

Difference between two arrays, PHP?

Posted on 2012-12-27
8
498 Views
Last Modified: 2012-12-28
Hi, I have two arrays:

array1=(200,201,202,203)
array2=(200,201)
 
What do I do to get the difference of the array to be (202,203). When I run the array_diff function I get the result as (200,201) and this is not my desired result. Thanks for the help.

A
0
Comment
Question by:aej1973
8 Comments
 
LVL 10

Accepted Solution

by:
honestman31 earned 50 total points
ID: 38726161
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Open in new window



result

Array
(
    [1] => blue
)


source

http://php.net/manual/en/function.array-diff.php
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 50 total points
ID: 38726162
array_idff will give diff only
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

refer
http://php.net/manual/en/function.array-diff.php

share your code to see in detail
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38726170
To anybody wanting a double-sided array_diff - mentioned by rudigier at noxx dot at. Remember, array_diff gives you everything in the first array that isn't in the subsequent arrays.

$array1=array('blue','red','green');
$array2=array('blue','yellow','green');

array_merge(array_diff($array1, $array2),array_diff($array2, $array1));

Result
------
Array
(
    [0] => red
    [1] => yellow
)
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38726197
Works fine from here so you'll have to show some code to see what you're doing differently.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:aej1973
ID: 38726214
Thank you for getting back to me. As I had mentioned in my question I tried array_diff and it does not work. I have attached my code for your reference. Is there any other way this can be done?
 $extens= $_GET['extsn']; //comma seperated values; 200,201
        $extss = explode(",",$extens); //puts it into an array
        echo "The nums to delete :".$extens;

        /**********get existing extens from the DB*********/

         $stmt = $db->query("SELECT val from queues where name='extension' and id='$group_number'");
         $row = $stmt->fetch(PDO::FETCH_ASSOC);
         $existing_exts= implode(",",$row); //200,201,202,203

         echo "Existing nos ".$existing_exts;
        /*****End************/


          $exten_array =array_diff($extss,$row);
          $exten_nos=implode(",",$exten_array);
          echo "difference is :". $exten_nos; //output is 200,201

Open in new window

0
 
LVL 10

Assisted Solution

by:ienaxxx
ienaxxx earned 50 total points
ID: 38726322
As per PHP manual, array_diff Returns an array containing all the entries from array1 that are not present in any of the other arrays.


But you can also build your own simple function like this:

function arrDiff($arr1, $arr2){
  foreach($arr1 as $key=>$value){
   if (!array_search($value, $arr2)){
     $result[]=$value;
   }
 }
if (count($result)>0){ // it will only return 0 if $result is NULL, if it's a string it will be 1!!!!
 return $result;
} else {
 return false;
}
}

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 38726766
Questions like this scream out for SSCCE.
<?php // RAY_temp_aej1973.php
error_reporting(E_ALL);
echo '<pre>';

// http://php.net/manual/en/function.array-diff.php

$array1 = array(200,201,202,203);
$array2 = array(200,201);

var_dump( array_diff($array1, $array2) );
var_dump( array_diff($array2, $array1) );

Open in new window

0
 

Author Comment

by:aej1973
ID: 38726853
Hello all, thank you for your inputs. I had made a mistake because of which array_diff was giving me a wrong output, it works now. Thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now