Home  >  Article  >  Backend Development  >  Delete a value element in an array_PHP tutorial

Delete a value element in an array_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:01:23965browse

Delete a certain value element in the array. This example uses the php array_diff function to delete a certain value element in the array. The method is very simple. Just use foreach and add the array_diff function. ​

Delete a value element in the array
This example is to use the php tutorial array_diff function to delete a certain value element in the array. The method is very simple. Just use foreach and add the array_diff function
*/

$a1 = array(array('blue','red','www.bkjia.com'),array('black','pink','green'));
$a2 = array('aaa','pink','bbbb');
$str = 'red';
$a2[] = $str;
foreach($a1 as $key => $value)
{  
$a1[$key] = array_diff($value,$a2);
}
print_r($a1);


//Simpler way

foreach (array_diff($a1, $a2) as $_key_1) {
          $arr_new[$_key_1] = $arr_1[$_key_1];
}


/*
The results are as follows:

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


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

)

array_diff syntax

array array_diff ( array $array1 , array $array2 [, array $ ... ] )

Returns the difference comparing 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
)


http://www.bkjia.com/PHPjc/445449.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445449.htmlTechArticleDelete a certain value element in the array. This example uses the php array_diff function to delete a certain value element in the array. Oh, the method is very simple. Use foreach and the array_diff function to delete a certain item in the array...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn