How to correctly implement PHP to delete duplicate elements from an array_PHP Tutorial

WBOY
Release: 2016-07-15 13:29:46
Original
1460 people have browsed it

Our actual operationThe PHP sample code for deleting duplicate elements from an array is as follows:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>function</FONT></STRONG></SPAN><SPAN> uniqueArray($array)   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>{   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>// Get unique elts as keys in assoc. array   </SPAN><LI class=""><SPAN> </SPAN><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></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$u_array[$array[$i]] = 1;   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>// Copy keys only into another array   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>reset($u_array, 1);   </SPAN><LI class=""><SPAN> </SPAN><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></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$unduplicated_array[] = key($u_array, 1);   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>next($u_array, 1);   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>}   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>return $unduplicated_array;   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>}   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span>   </span></span></li>
<li class=""><span> </span></li>
</ol>
Copy after login

The above string of code is the specific implementation method of PHP deleting duplicate elements from an array.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446339.htmlTechArticleOur actual operation of PHP to delete duplicate elements in an array is as follows: ? function uniqueArray($array) { //Getuniqueeltsaskeysinassoc .array for($ i = 0 ,$ n = count ($array,1);$i $n...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!