We help IT Professionals succeed at work.

How to delete values from an array using php

Lennart Ericson
on
245 Views
Last Modified: 2017-03-10
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?
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Only recommendation I would make over Peos John's reply is to rather use the www.php.net 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

http://be2.php.net/manual/en/function.array-diff.php
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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:
http://php.net/manual/fr/function.array-unique.php#81513
http://php.net/manual/en/function.array-slice.php#112359
http://php.net/manual/en/function.array-diff.php#68623  <<< Especially this one!

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions