Home >Backend Development >PHP Tutorial >php definition, traversal and deletion of arrays
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 Realize the clearing of pictures that have not been accessed within a fixed date
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!