Home > Backend Development > PHP Tutorial > PHP implements the method of searching for one-dimensional array elements and deleting the corresponding elements of the two-dimensional array, dimension two-dimensional array_PHP tutorial

PHP implements the method of searching for one-dimensional array elements and deleting the corresponding elements of the two-dimensional array, dimension two-dimensional array_PHP tutorial

WBOY
Release: 2016-07-13 09:47:09
Original
852 people have browsed it

php implements the method of searching for one-dimensional array elements and deleting the corresponding elements of the two-dimensional array, dimension two-dimensional array

The example of this article tells the php implementation of searching for the one-dimensional array elements and deleting them Method for corresponding elements of a two-dimensional array. Share it with everyone for your reference. The details are as follows:

Define a one-dimensional array and a two-dimensional array as follows

$fruit=array('apple','orange');
$products = array( array('name'=>'apple','price'=>23.4),
array('name'=>'orange','price'=>45.3),
array('name'=>'biscuit','number'=>5,'price'=>34)
);

Copy after login

You need to find out whether the elements in the $products array intersect with the $fruit elements in the array. If so, keep them, otherwise delete them.

The implementation method is:

foreach($products as $key=>$value)
{
   if(!in_array($value["name"],$fruit))
   unset($products[$key]);
}
array_values($products);
//使用unset()销毁数组元素时候应注意索引问题最好使用array_values()给数组重新排序

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1027493.htmlTechArticlephp realizes the method of searching one-dimensional array elements and deleting the corresponding elements of the two-dimensional array, the dimension of the two-dimensional array is an example in this article Describes the PHP implementation of searching for one-dimensional array elements and deleting the corresponding elements of the two-dimensional array...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template