Home  >  Article  >  Backend Development  >  php array_diff 用法

php array_diff 用法

WBOY
WBOYOriginal
2016-06-23 14:34:33901browse

<p>我们在数组a中,想把1,2两个值去掉的话,该怎么办呢?有一个传统方法就是遍历数组,如果值等于1,或者等2我们就移除这个元素,显然比较麻烦,如今就不同了,我们可以这样来写</p><p>首先,把我们不需要的数值组成一个数组,</p>

  
  
          <p class="sycode">              <pre class="sycode" name="code">$del=array(1,2);

<p>接下来我们就利用我们的主角array_diff函数了</p>

          <p class="sycode">              <pre class="sycode" name="code">$d=array_diff($a,$del);var_dump($d);

<p>php的执行结果就会是我们梦寐以求的这样哦:</p>

          <p class="sycode">              <pre class="sycode" name="code">array(1) {  [2]=>  int(3)}
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
Previous article:MVC scheme with php implementNext article:php var_dump()函数