PHP array functions (traversal, sort)

Source: Internet
Author: User
First, traverse

    • Foreach
      foreach (array_expression as $value) and foreach (array_expression as $key = = $value)
    • List () and each (): list () can only be used for arrays of numeric indexes, and the numeric index starts at 0. Each () returns the key names and corresponding values in the array, and moves the array pointer forward.
    •    
          "PHP24 class", "1" and "JAVA24 Class", "2" and "VB24", "3" and "VC24 class"); while (List ($name, $value) =each ($array)) {// Use the list function to get the values of the returned array in each function, assign to $name and $value, and then use the while loop to output echo $name = $value. "
      ";//Output list function gets the key name and value}?>
Second, commonly used functions
Number of statistics array int count (mixed var)
  • Add an element to an array: Array_push () adds the passed-in element to the end of the array and returns the total number of new cells in the array
  • int Array_push (array array,mixed var)//array is the specified array, VAR is the value in the pressed array
  • Gets the last element in the array: Array_pop () returns the last element in the array, minus 1 of the array length, or null if the array is empty (or not an array). Mixed Array_pop (array array)
  • Delete duplicate elements in an array: array array_unique (array array)
  • Delete an element in an array: unset (mixed arr[*])
  •  Gets the key name of the specified element in the array mixed Array_search (mixed Needle,array haystack[, bool Strict])//If the queried element appears more than two times in the array, the first matching key name is returned//needle: Specifies the value of the search in the array// Haystach: Specifies the array to be searched//strict: Optional parameter, if true, the type of needle is checked in haystack 
    array Array_keys (array input[, mixed search_value[, bool strict])// Returns all matching key names in the input array 
     
     
    Pre class= "Brush:php;toolbar:false" >
     
     
     
     three, sort 
    sort () implements arrays from low to high, and strings are sorted in ASCII order. 
  • BOOL Sort (array &array[, int sort_flags])//sort_flags specifies how to sort: sort_regular (default), Sort_numeric (compares elements as numbers), Sort_ String (compares the element as a string)
  • Rsort (): Used to sort the array from high to low.
     
     
       
       
      1. The above describes the PHP array functions (traversal, sorting), including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

      2. 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.