Home  >  Article  >  Backend Development  >  The role of PHP function array_flip() in deleting duplicate array elements_PHP tutorial

The role of PHP function array_flip() in deleting duplicate array elements_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:29:521141browse

We all know, PHP function array_flip() format:

<ol class="dp-xml"><li class="alt"><span><span>array array_flip ( array trans ) <br>//array_flip -- 交换数组中的键和值 </span></span></li></ol>

array array_flip ( array trans ) //array_flip -- exchange the keys and values ​​in the array

as follows:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">arr</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(…………) ;//假设有一万个元素的数组,里面有重复的元素。   </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">arr</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_flip</font></span><span>(array_flip($arr)); //这样便可以删除重复元素。 </span>
</li>
</ol>

What on earth is going on? Let’s take a look at the role of array_flip(): The PHP function array_flip() is used to exchange the key and value of each element of an array, such as:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">arr1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> ("age" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 30, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "php自学网");   </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">arr2</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_flip</font></span><span>($arr1); //$arr2 就是 array(</span><span class="attribute"><font color="#ff0000">30</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "age", "php自学网" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "name");  </span>
</li>
</ol>

In the PHP array, different elements are allowed to take the same value, but the same key name is not allowed to be used by different elements. Such as:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">arr1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> ("age" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 30, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "php自学网", "age" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 20); "age" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 20将会取代"age" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 30   </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">arr1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> ("name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "php自学网", "age" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 45);   </span>
</li>
</ol>

Here$ arr1 and $arr2 are equal.

So, we can know why array_flip(array_flip($arr)) can delete duplicate elements in the array. First, the value in $arr will become a key name, because the value is repeated. After becoming a key name, these repeated values ​​will become duplicate key names. The PHP engine will delete the duplicate key names and only keep the last one. . Such as:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">arr1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> ("age" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 30, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "php自学网", "age" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 20);   </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">arr1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_flip</font></span><span>($arr1); //$arr1 变成了 array("php自学网" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "name", </span><span class="attribute"><font color="#ff0000">20</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "age");   </span>
</li>
<li class="alt"><span>//再把 $arr1 的键名与值还复:   </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">arr1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_flip</font></span><span>($arr1);   </span>
</li>
</ol>

The code for the PHP function array_flip() above is written more concisely:

<ol class="dp-xml"><li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">arr1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_flip</font></span><span>(array_flip($arr1));   </span></span></li></ol>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446336.htmlTechArticleWe all know that the PHP function array_flip() format: arrayarray_flip(arraytrans) //array_flip--exchange the array Key and value array array_flip (array trans) //array_flip -- swap array...
Statement:
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