>백엔드 개발 >PHP 튜토리얼 >如何正确实现PHP删除数组重复元素_PHP教程

如何正确实现PHP删除数组重复元素_PHP教程

WBOY
WBOY원래의
2016-07-15 13:29:461487검색

我们在实际操作PHP删除数组重复元素示例代码如下:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span>   </span></span></li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="tag-name"><strong><font color="#006699">function</font></strong></span><span> uniqueArray($array)   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>{   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// Get unique elts as keys in assoc. array   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>for ($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>,$</span><span class="attribute"><font color="#ff0000">n</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">count</font></span><span>($array, 1);$i</span><span class="tag"><strong><font color="#006699"></font></strong></span><span>$n;$i++)   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>$u_array[$array[$i]] = 1;   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// Copy keys only into another array   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>reset($u_array, 1);   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>for ($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>,$</span><span class="attribute"><font color="#ff0000">n</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">count</font></span><span>($u_array, 1);$i</span><span class="tag"><strong><font color="#006699"></font></strong></span><span>$n;$i++) {   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>$unduplicated_array[] = key($u_array, 1);   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>next($u_array, 1);   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>return $unduplicated_array;   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
<li class=""><span> </span></li>
</ol>

上面这一串代码就是PHP删除数组重复元素的具体实现方式。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446339.htmlTechArticle我们在实际操作 PHP删除数组重复元素示例代码如下: ? function uniqueArray($array) { //Getuniqueeltsaskeysinassoc.array for($ i = 0 ,$ n = count ($array,1);$i $n...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.