Delete a value element in the array _ PHP Tutorial

Source: Internet
Author: User
Deletes a value element from an array. Delete a value element in the array. This instance uses the phparray_diff function to delete a value element in the array, the method is simple. use the foreach and array_diff functions to delete a value element from an array. In this example, use the php array_diff function to delete a value element from the array, the method is simple. add the array_diff function with foreach.

Deletes a value element from an array.
This example uses the array_diff function in the php Tutorial to delete a value element in the array. the method is simple: use foreach and add the array_diff function.
*/

$ A1 = array ('blue', 'red', 'www .bkjia.com '), array ('black', 'Pink', 'green '));
$ A2 = array ('AAA', 'Pink', 'bbbbb ');
$ Str = 'red ';
$ A2 [] = $ str;
Foreach ($ a1 as $ key => $ value)
{
$ A1 [$ key] = array_diff ($ value, $ a2 );
}
Print_r ($ a1 );


// Simpler method

Foreach (array_diff ($ a1, $ a2) as $ _ key_1 ){
$ Arr_new [$ _ key_1] = $ arr_1 [$ _ key_1];
}


/*
The result is as follows:

Array
(
[0] => array
(
[0] => blue
[2] => yellow
)


[1] => array
(
[0] => black
[2] => green
)


)

Array_diff syntax

Array array_diff (array $ array1, array $ array2 [, array $...])
Returns a difference between array1 and array2.
*/
$ Array1 = array ("a" => "green", "php100.com", "blue", "red ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_diff ($ array1, $ array2 );

Print_r ($ result );

/*
Array
(
[1] => blue
)


The array_diff function is used to delete a value element in the array. the method is simple: use the foreach function and the array_diff function to delete a value element in the array...

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.