Analysis of the specific method of merging arrays with the PHP function array_merge()_PHP tutorial

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

We may If the original array itself has been reordered, then after the array merging is completed, you must reorder the resulting new array. The following example introduces how to use the PHP function array_merge():

<ol class="dp-xml">
<li class="alt"><span><span><? /* 首先建立第一个数组 */   </span></span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">fruitArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("apple", "orange", "banana", "Peach", "pear");    </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="attribute"><font color="#ff0000">vegArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("carrot", "green beans", "asparagus", "artichoke", "corn");    </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 现在使用array_merge()函数将两个数组合并成一个新的数组 */   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">goodfoodArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_merge</font></span><span>($fruitArray, $vegArray);    </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 最后我们把合并后的新数组的所有元素的键(key)与值(value)都显示在网页上 */   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>while (list($key,$value) = each($goodfoodArray)) {   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>echo "$key : $value<br>";   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}    </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>?>     </span></li>
</ol>
Copy after login

The displayed results are as follows:

0 : apple

1 : orange

2 : banana

3 : Peach

4 : pear

5 : carrot

6 : green beans

7 : asparagus

8: artichoke

9: corn

The usage of PHP function array_merge() in array merging is as shown above.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446379.htmlTechArticleWe may be concerned that if the original array itself has been reordered, then after the array merging is completed, you must The new array generated is re-sorted. Here’s an example...
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!