tags:

views:

41

answers:

2

Hello, I have cafeid=(1,2,3,4,5,6,7) and checkid= (1,3,5)

How can I show the output only (2,4,6,7)?

Thank you for your answers.

+5  A: 

By using

  • array_diff — Computes the difference of arrays

Example:

$cafeid  = array(1,2,3,4,5,6,7);
$checkid = array(1,3,5);
print_r( array_diff($cafeid, $checkid) );

will give:

Array
(
    [1] => 2
    [3] => 4
    [5] => 6
    [6] => 7
)
Gordon
+2  A: 

You can use the array_diff function to return the values that are present in the first array, and not in the second one.


As an example, in your situation, this portion of code :

$cafeid = array(1,2,3,4,5,6,7);
$checkid = array(1,3,5);
var_dump(array_diff($cafeid, $checkid));

will get you this kind of output :

array
  1 => int 2
  3 => int 4
  5 => int 6
  6 => int 7


As an advice : there are a lot of useful function that allow one to manipulate arrays and work with them ; you should take a quick look at the list of those functions : Array Functions.

I'm pretty sure this'll be useful one day or another ;-)

Pascal MARTIN
I was 2 minutes faster ;P
Gordon
Damn ^^ There should be a badge for being faster ^^ *(I would sometimes get it -- but not every time, it seems ^^ )*
Pascal MARTIN