I have two arrays named rows and contacts.
The first array rows is like :
Array
(
[0] => email@gmail.com
[1] => test@gmail.com
[2] => tester@gmail.com
[3] => vin@gmail.com
)
The second array contacts is as :
Array
(
[test@gmail.com] => te开发者_如何学Cst@gmail.com
[ram@gmail.com] => Ram
[vin@gmail.com] => Vinay
[man_test@yahoo.com] => Manoj
[homan@rediffmail.com] => Homan
)
What I want is the contacts array to be as :
Array
(
[ram@gmail.com] => Ram
[man_test@yahoo.com] => Manoj
[homan@rediffmail.com] => Homan
)
Edit
I tried some functions like array_diff(), array_keys() etc. but they are not giving me the desired output, may be I am not able to use them correctly....!
I don't want to use loop for this purpose because the given arrays are only sample data but in real they are very huge.
Please help.....
Thanks in Advance.....
Another way:
$contacts = array_diff_key($contacts, array_flip($rows));
Assuming I understand the question correctly, you could do that:
for ($i = 0; $i < count($rows); $i++) {
$s = $rows[$i];
unset($contacts[$s]);
}
foreach ($contacts as $email => $name) {
if (!in_array($email, $rows)) {
$contact[$email] = $name;
}
}
The new array is $contact
, not $contacts
, as your question asked for before you edited it.
As Felix Kling and Dan Grossman's answers will work on the examples you have shown, you probably have whitespace issues like newlines in your data, especially if you have extracted it from a file. So, extending Felix's answer:
$contacts = array_diff_key(array_map('trim', $contacts), array_flip(array_map('trim', $rows)));
You could probably make it more efficient by constructing your data correctly in the first place, but this should do the trick.
http://codepad.org/RIvLbyJy
精彩评论