Home  >  Article  >  Backend Development  >  How to delete values ​​in a two-dimensional array in php

How to delete values ​​in a two-dimensional array in php

王林
王林Original
2020-11-09 09:16:242849browse

php method to delete values ​​in a two-dimensional array: you can use the foreach statement combined with the unset function to delete, such as [foreach($array as $key=>$value){if($key== 'a')[unset($array[$key]);]}].

How to delete values ​​in a two-dimensional array in php

The unset() function is used to clear and destroy variables. For unused variables, we can use unset() to destroy elements.

(Learning video recommendation: java video tutorial)

The same principle as deleting a one-dimensional array, you can delete the corresponding element through the key name of the array.

Example:

<?php
$array = array(&#39;a&#39;=>array(&#39;a1&#39;,&#39;a2&#39;),&#39;b&#39;=>array(&#39;b1&#39;,&#39;b2&#39;));
foreach($array as $key=>$value){
if($key == &#39;a&#39;)[
unset($array[$key]);
}
//或者删除二维数组中二维中的元素
if($key == &#39;a&#39;)[
unset($array[$key][0]);
}
}
print_r($array);
?>

Related recommendations:php training

The above is the detailed content of How to delete values ​​in a two-dimensional array in php. 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