Home >Backend Development >PHP Tutorial >The role of PHP array functions in our coding_PHP tutorial

The role of PHP array functions in our coding_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:30:02866browse

If you have a large array and you need to find out whether it contains a specific element, you can use the PHP array function in_array(). The following example will display "Not found in this array" because it is looking for Albert in an array named $namesArray, and such an element does not exist in the $namesArray array.

<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 class=""><SPAN> </SPAN><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></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>if (in_array($lookingFor, $namesArray)) {   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>echo "You've found it!";   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>} else {   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>echo "Not found in this 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></ol>

If you change the value of $lookingFor to Mary, you will get the message "You've found it!" because Mary is an element in the $namesArray array.

If you want to count the number of elements in an array, simply use the PHP array function 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 class=""><SPAN> </SPAN><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></span></li></ol>

The value of $count returned is 7.

You can add elements at the beginning or end of an array. You can also use the PHP array function array_merge() to create a new array containing the elements in two or more arrays. When merging, the order of the elements will be as specified. Sequential arrangement, if the original array was sorted, it needs to be reordered after merging.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446331.htmlTechArticleIf you have a large array and you need to find out whether it contains a specific element, you can use PHP array function in_array(). The example below will display "Not found in this...
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