Rumah > Artikel > pembangunan bahagian belakang > 如何正确实现PHP删除数组重复元素_PHP教程
我们在实际操作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删除数组重复元素的具体实现方式。