Home > php教程 > php手册 > PHP数组函数对我们编码中的作用体现

PHP数组函数对我们编码中的作用体现

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:07:01
Original
1171 people have browsed it

如果有一个很大的数组,而你需要找出其中是否包含一个特定的元素,就可以使用PHP数组函数in_array()。下面的例子将显示“Not found in this array”,因为在一个名字为$namesArray的数组中查找Albert,而在$namesArray数组中不存在这样一个元素。

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> $</span><span class="attribute"><font color="#ff0000">namesArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");   </span></span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">lookingFor</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"Albert"</font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>if (in_array($lookingFor, $namesArray)) {   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>echo "You've found it!";   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>} else {   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>echo "Not found in this 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>
</ol>
Copy after login

如果把$lookingFor的值改为Mary,就会得到“You've found it!”的信息,因为Mary是$namesArray数组中的一个元素。

如果要对一个数组中的元素个数进行计数,只要简单地使用PHP数组函数count()即可:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> $</span><span class="attribute"><font color="#ff0000">namesArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");   </span></span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">count</font></span><span class="attribute-value"><font color="#0000ff">count</font></span><span> = count($namesArray); </span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
</ol>
Copy after login

返回的$count的值为7。

可以在一个数组的开头或结尾处添加元素,还可以使用PHP数组函数array_merge()来建立一个包含二个或更多数组中元素的新数组,合并时,元素的顺序会按指定的顺序排列,如果原来的数组是被排过序的,在合并后需要对它重新排序。


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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template