RE: [PHP] RE: Function to compare an array with another array
| From: | Stewart Taylor | Date: | Thu, 15 Mar 2001 14:20:57 +0000 |
| Subject: | RE: [PHP] RE: Function to compare an array with another array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-44028@lists.php.net to get a copy of this message | ||
How about using the array_diff function instead.
array_diff ($array1,$array2) = elements in $array1 not in $array2;
array_diff ($array2,$array1) = elements in $array2 not in $array1;
From manual:
array array_diff (array array1, array array2 [, array ...])
Array_diff() returns an array containing all the values of array1 that are
not present in any of the other arguments. Note that keys are preserved.
Example 1. Array_diff() example
$array1 = array ("a" => "green", "red", "blue");
$array2 = array ("b" => "green", "yellow", "red");
$result = array_diff ($array1, $array2);
This makes $result have array ("blue");
-----Original Message-----
From: Fang Li [mailto:fli@biographix.com]
Sent: 15 March 2001 14:03
To: Tim Ward; 'Jimmy Bäckström'; PHP General List
Subject: RE: [PHP] RE: Function to compare an array with another array
Hi,
I am trying to use the Function ArrayCompare, but it always has an error in
"if(($key = array_search($element, $array2)) == false)", and also I couldn't
get the page of http://www.stivesdirect.com/e-mail-disclaimer.html
Would you please give me a clue? Thanks!
Fang
-----Original Message-----
From: Tim Ward [mailto:tim.ward@stivesdirect.com]
Sent: March 5, 2001 5:48 AM
To: 'Jimmy Bäckström'; PHP General List
Subject: [PHP] RE: Function to compare an array with another array
function ArrayCompare($array1, $array2)
{ $answer = new Array()
foreach ($array1 as $element)
{ if (($key = array_search($element, $array2)) === false)
{ $answer[$key] = $array2[$key];
}
}
return $answer;
}
this will return an array that is a copy of the first array with only those
elements that are not in array2. If you only want the first one either just
use the first one( foreach(){ ... break;}) or put a break in the if.
Tim Ward
Senior Systems Engineer
Please refer to the following disclaimer in respect of this message:
http://www.stivesdirect.com/e-mail-disclaimer.html
> -----Original Message-----
> From: Jimmy Bäckström [mailto:broder_b@chello.se]
> Sent: 05 March 2001 06:38
> To: PHP General List
> Subject: Function to compare an array with another array
>
>
> Shu!
> I need some help here.
> I have a script where I want to compare two arrays with
> eachother, and return one value from that does not exists in
> both arrays. I have an array of pictures id number created
> from a database query. I use sessions to save id numbers of
> pictures into an array. I want the first array to be compared
> to the second and return the first key/value-pair that are
> not found in the session array...
> Is there a way I can do this?
>
> Thankful for any help!
> /Broder B
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net