How to delete values from an array using php

Lennart Ericson
Lennart Ericson used Ask the Experts™
I have two arrays, $array_1 and $array_2.
If a value in $array_2 is also found in $array_1 it should be deleted from $array_1.
The same value might be found several times in both of the arrays. Is the value present in $array_2, regardless of number of appearances, it should be deleted from all appearances in $array_1.
Script language: php
What procedure do you recommend?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can use array_diff function for this.

Value  green is twice in below example.


Most Valuable Expert 2017
Distinguished Expert 2018

Only recommendation I would make over Peos John's reply is to rather use the resource as a reference. W3 Schools is ok but PHP.Net is the definitive source and has a wealth of other related information in the examples, topic posts and related function lists
Most Valuable Expert 2011
Top Expert 2016

No points for this, please, but I would add that sometimes the most useful part of the online PHP manual are the user-contributed notes in the function reference.  Examples:  <<< Especially this one!


Sorry for late reply.
Thanks so much. It worked very well.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial