Rumah  >  Artikel  >  pembangunan bahagian belakang  >  如何正确实现PHP删除数组重复元素_PHP教程

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

WBOY
WBOYasal
2016-07-15 13:29:461460semak imbas

我们在实际操作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...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn