Question: I have a script that has two arrays, the initial data array (`$initial`), and the modified data array (`$modified`), but I have hit an issue in detecting the differences between the arrays. I need to determine which values (the keys will be different between them) have been added, and which values have been removed and place the changes into two arrays afterward (`$dataAdded` and `$dataRemoved`).

Are there any functions available to do this, or if not, are there any concepts that I could follow?

Answer: 
[array\_diff()](http://docs.php.net/function.array-diff) can do that.

```
<?php
$initial = array(1,2,3,4,5,6,7,8);
$modified = foo($initial);

$removed = array_diff($initial, $modified);
$added = array_diff($modified, $initial);

echo 'removed=', join(', ', $removed), "\n";
echo 'added=', join(', ', $added), "\n";

function foo($a) {
    // "removing" all odd numbers
    // and "adding" 10,12,14,16
    return array_map(function($e) { return $e*2; }, $a);
}

```

prints

```
removed=1, 3, 5, 7
added=10, 12, 14, 16

```
I was going to cancel the question, but I felt there might be useful to others having a similar problem, or if there is something foolish in here that I have missed, but anyway:

I used the following:

```
$dataAdded = array();
$dataRemoved = array();
foreach ($modified as $data) {
    if (!in_array($data, $initial)) {
        $dataAdded[] = $data;
    }
}
foreach ($initial as $data) {
    if (!in_array($data, $modified)) {
        $dataRemoved[] = $data;
    }
}

```