php array_diff 用法

WBOY
Release: 2016-06-23 14:34:33
Original
901 people have browsed it

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

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

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

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

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

          <p class="sycode">              <pre class="sycode" name="code">array(1) {  [2]=>  int(3)}
Copy after login

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