?
Solved

PHP- I need to compare two arrays and find the values that match between them

Posted on 2006-04-04
4
Medium Priority
?
431 Views
Last Modified: 2008-02-01
Say i have 2 arrays
array 1 = 0-45, 1-14, 2-16, 3-25, 4-99

array 2 = 0-16 1-15 2-67 3-45 4-88

Is there a simple PHP function to return which values match in another array? In this case a comparative array that returns

0-16
1-45

since both arrays have those values?

Thanks!

0
Comment
Question by:stormist
  • 2
4 Comments
 
LVL 3

Expert Comment

by:zreak
ID: 16378664
array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

Example 227. array_intersect() example

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

This makes $result have

<?php Array
(
   [a] => green
   [0] => red
) ?>
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 1000 total points
ID: 16378668
0
 
LVL 10

Author Comment

by:stormist
ID: 16378886
Hi all! What would be a way to use that function but reset the keys? first one 0 , 2nd one 1. etc?

Thx!
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 16378999
Use: http://pl2.php.net/manual/en/function.array-values.php
on the resulted array. Resulting array would be indexed by numbers.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 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