Jump to content

[SOLVED] Comparing two arrays and extract the differences


danrah

Recommended Posts

I have two one-dimensional arrays starting from index 0. The first array stores the complete list of students' names and has (for example) 500 elements and the second array consists of a part of the larger array. How can i extract the elements that exist in the first array and not in the second array.

Thanks in advance,

Use array_diff()

 

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

 

Result:

Array
(
    [1] => blue
)

 

Manual: http://uk3.php.net/manual/en/function.array-diff.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.