PHP array search implementation

Source: Internet
Author: User

Array_unshift () // Add an element to the array header, that is, insert a new element into the array, and the corresponding index is automatically changed.
Array_push () // Add an element at the end of the array
Array_shift () // delete an element in the array Header
Array_pop () // delete an element at the end of the array

The usage of these functions is almost the same. You can get the specific usage in the PHP help manual.

PHP code example for searching Arrays:

The in_array () function searches for a specific value in the array. If it is found, TRUE is returned. Otherwise, FALSE is returned. The following example is from the PHP help manual.

 
 
  1. < ?php  
  2. $os = array("Mac", "NT", "Irix", "Linux");  
  3. if (in_array("Irix", $os)) {  
  4. echo "Got Irix";  
  5. }  
  6. if (in_array("mac", $os)) {  
  7. echo "Got mac";  
  8. }  
  9. ?> 

In particular, in_array () is distinguished by size during search, so the above example only returns "Got Irix ". You can also use arrays as substrings to search in arrays. For more examples, see the help manual.

Search join array for PHP search Array

If the search array is an associated array, array_key_exists () is used. The returned value is the same as that of in_array. We also reference the PHP help manual example to illustrate:

 
 
  1. < ?php  
  2. $search_array = array('first' => 1, 'second' => 4);  
  3. if (array_key_exists('first', $search_array)) {  
  4. echo "The 'first' element is in the array";  
  5. }  
  6. ?> 

I believe that this piece of code should not be explained. The result is as follows: the 'first 'element is in The array. This indicates that "first" exists in the array "search_array.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.