Solved

Difference between two arrays, PHP?

Posted on 2012-12-27
8
502 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 83

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 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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 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