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
Solved

Difference between two arrays, PHP?

Posted on 2012-12-27
8
504 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
IP 10.0.1.2 / 255.0.0.0 61 56
PHP delete contents of file- before writing to it 6 22
Generate PDF from MySQL using PHP 3 21
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

861 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