php definition, traversal and deletion of arrays

墨辰丷
Release: 2023-03-31 06:36:02
Original
1637 people have browsed it

This article mainly introduces the definition, traversal and deletion of arrays in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The example of this article describes how PHP implements searching for one-dimensional array elements and deleting the corresponding elements of the two-dimensional array. 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

It is necessary to find out whether the element in the $products array has an intersection with the $fruit element in the array. If so, keep it, otherwise delete it.

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php uses the serial port to realize the idea of ​​sending text messages and the specific implementation

PHP Realize the clearing of pictures that have not been accessed within a fixed date

PHP realizes functions such as batch uploading of files, setting file suffix names, modifying file names, etc.

The above is the detailed content of php definition, traversal and deletion of arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!