Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Difference between two arrays, PHP?

Posted on 2012-12-27
8
Medium Priority
?
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 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