Home >Backend Development >PHP Tutorial >php definition, traversal and deletion of arrays

php definition, traversal and deletion of arrays

墨辰丷
墨辰丷Original
2018-06-09 09:23:101658browse

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)
);

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()给数组重新排序

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!

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