Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Difference between two arrays, PHP?

Posted on 2012-12-27
8
Medium Priority
?
514 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 200 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 84

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
 

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 200 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

926 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