PHP array functions (traversal and sorting)

Source: Internet
Author: User
: This article mainly introduces PHP array functions (traversal and sorting). For more information about PHP tutorials, see. I. traversal

  • Foreach
    Foreach (array_expression as $ value) and foreach (array_expression as $ key => $ value)
  • List () and each (): list () can only be used in an array of numeric indexes, and the number index starts from 0. Each () returns the key name and corresponding value in the array, and moves the array pointer forward.
  •    "PHP 24 classes", "1" => "Java 24 classes", "2" => "VB24 classes", "3" => "VC24 classes "); while (list ($ name, $ value) = each ($ array) {// use the list function to obtain the value of the array returned by the each function, and assign them to $ name and $ value respectively, and then use the while loop to output echo $ name = $ value."
    "; // Output the key name and value obtained by The list function}?>
II. common functions
Count the number of arrays int count (mixed var)
 
  • Add an element to the array: array_push () adds the incoming element to the end of the array, and returns the total number of new elements in the array.
  • Int array_push (array, mixed var) // array is the specified array, and var is the value in the array.
  • Obtain the last element in the array: array_pop (). return the last element in the array and reduce the length of the array by 1. if the array is empty (or not an array), return null. Mixed array_pop (array)
  • Delete repeated elements in the array: array array_unique (array)
  • Delete an element in the array: unset (mixed arr [*])
  • Obtain 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 twice in the array, returns the first matched key name // needle: specify the search value in the array // haystach: specify the search array // strict: optional parameter, if true, check the needle type in haystack.
    Array array_keys (array input [, mixed search_value [, bool strict]) // returns all matching key names in the input array.
     
     
      III. sorting
      Sort () is used to sort arrays from low to high, and string types are sorted in the order of ASCII codes.
    1. Bool sort (array & array [, int sort_flags]) // sort_flags specifies the sorting method: SORT_REGULAR (default), SORT_NUMERIC (compare the elements as numbers ), SORT_STRING (compare elements as strings)
    2. Rsort (): used to sort arrays from high to low.
       
       
         
         
        1. The above introduces PHP array functions (traversal and sorting), including the content, hope to be helpful to friends who are interested in PHP tutorials.

          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.