PHP array_diff usage

Source: Internet
Author: User

In array a, what should we do if we want to remove values 1 and 2? One traditional method is to traverse the array. If the value is equal to 1, or wait for 2, we will remove this element. It is obviously troublesome. Now it is different. We can write it like this.

First, we need to make up an array of unnecessary values,



$ Del = array (1, 2 );

Next we will use our role array_diff function.

$ D = array_diff ($ A, $ del); var_dump ($ D );

PHP Execution results will be what we dream:

Array (1) {[2] => int (3 )}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.