Home  >  Article  >  Backend Development  >  Introduction to the implementation method of PHP search array_PHP tutorial

Introduction to the implementation method of PHP search array_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:33:16911browse

array_unshift() // Add an element to the head of the array, that is, insert a new element into the array, and the corresponding index will automatically change
array_push() //Add elements to the end of the array
array_shift() //Delete elements to the head of the array
array_pop() //Delete elements to the end of the array

The usage of these functions is basic It's almost the same. You can get the specific usage in the PHP help manual, so I won't go into details here.

Code example of PHP searching array:

in_array() function searches for a specific value in the array and returns TRUE if found, otherwise it returns FALSE. The following examples are from the PHP help manual.

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>$</span><span class="attribute">os</span><span> = </span><span class="attribute-value">array</span><span>("Mac", "NT", "Irix", "Linux");  </span></li><li class="alt"><span>if (in_array("Irix", $os)) {  </span></li><li><span>echo "Got Irix";  </span></li><li class="alt"><span>}  </span></li><li><span>if (in_array("mac", $os)) {  </span></li><li class="alt"><span>echo "Got mac";  </span></li><li><span>}  </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>

It should be noted that in_array() is size-sensitive when searching, so the above example will only return "Got Irix". You can also search within an array using an array as a substring, see the help manual for more examples.

PHP search array search associative array

If the array you search is an associative array, you need to use array_key_exists(), its return value is the same as in_array() . Similarly, we quote the example from the PHP help manual to illustrate:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>$</span><span class="attribute">search_array</span><span> = array('first' =</span><span class="tag">></span><span> 1, 'second' =</span><span class="tag">></span><span> 4);  </span></span></li>
<li class="alt"><span>if (array_key_exists('first', $search_array)) {  </span></li>
<li><span>echo "The 'first' element is in the array";  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>

I believe there is no need to explain such a piece of code. You can understand it at a glance. The result: The 'first' element is in the array. This means "first" exists in the array "search_array".


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446069.htmlTechArticlearray_unshift() //Add an element to the head of the array, that is, insert a new element into the array. The corresponding index also automatically changes array_push() //Add element array_shif at the end of the 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