Rumah >pembangunan bahagian belakang >tutorial php >PHP函数array_flip()在重复数组元素删除中的作用_PHP教程
我们都知道,PHP函数array_flip()格式:
<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 -- 交换数组中的键和值
方法如下:
<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>
究竟是怎么回事呢?来看下array_flip()的作用:PHP函数array_flip()用于将一个数组的每个元素的键和值交换,如:
<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>
在PHP的数组中,允许不同的元素可以取同一个值,但不允许同一个键名被不同的元素使用,如:
<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>
这里 $arr1与$arr2 是相等的。
于是,我们便可以知道,为什么 array_flip(array_flip($arr)) 可以删除数组中重复的元素了。首先,$arr里的值会变成键名,因为值是有重复的,变成键名之后这些重复的值便成了重复的键名,PHP引擎将重复的键名删除,只保留最后一个。如:
<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>
上面PHP函数array_flip()的代码写得简洁一些就是:
<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>