Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP | asort() Function
Next article icon

PHP | asort() Function

Last Updated : 09 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
The asort() function is an inbuilt function in PHP which is used to sort an array according to values. It sorts in a way that relation between indices and values is maintained. By default it sorts in ascending order of values. Syntax:
bool asort( $array, $sorting_type )
Parameters: This function accepts two parameters as mentioned above and described below:
  • $array: This parameter specifies the array which to be sort. It is a mandatory parameter.
  • $sorting_type: This is an optional parameter. There are different sorting types which are discussed below:
    • SORT_REGULAR: The value of $sorting_type is SORT_REGULAR then items are compare normally.
    • SORT_NUMERIC: The value of $sorting_type is SORT_NUMERIC then items are compares numerically.
    • SORT_STRING: The value of $sorting_type is SORT_STRING then items are compares as string.
    • SORT_LOCALE_STRING: The value of $sorting_type is SORT_STRING then items are compares as string, based on current locale.
Return Value: This function returns True on success or False on failure. Below programs illustrate the asort() function in PHP. Program 1: php
<?php // PHP program to illustrate // asort() function  // Input different array elements $arr = array("0" => "Web Technology",             "1" => "Machine Learning",             "2" => "GeeksforGeeks",             "3" => "Computer Graphics",             "4" => "Videos",             "5" => "Report Bug",             "6" => "Article",             "7" => "Sudo Placement",             "8" => "SContribute",             "9" => "Reset",             "10" => "Copy",             "11" => "IDE",             "12" => "Gate Note",         );  // Implementation of asort() asort($arr);  // for-Loop for displaying result foreach ($arr as $key => $val) {     echo "[$key] = $val";     echo"\n"; }  ?> 
Output:
 [6] = Article [3] = Computer Graphics [10] = Copy [12] = Gate Note [2] = GeeksforGeeks [11] = IDE [1] = Machine Learning [5] = Report Bug [9] = Reset [8] = SContribute [7] = Sudo Placement [4] = Videos [0] = Web Technology 
Program 2: php
<?php // PHP program to illustrate // asort() function      // Input different array elements $arr = array("a" => 11,             "b" => 22,             "d" => 33,             "n" => 44,             "o" => 55,             "p" => 66,             "r" => 77,             "s" => 2,             "q" => -11,             "t" => 3,             "u" => 1000,             "z" => 1,                                      ); // Implementation of asort() asort($arr);  // for-Loop for displaying result foreach ($arr as $key => $val) {     echo "[$key] = $val";     echo"\n"; }  ?> 
Output:
 [q] = -11 [z] = 1 [s] = 2 [t] = 3 [a] = 11 [b] = 22 [d] = 33 [n] = 44 [o] = 55 [p] = 66 [r] = 77 [u] = 1000 
Related Articles:
  • sort() Function
  • arsort() Function
  • uksort() Function
  • usort() Function
Reference: http://php.net/manual/en/function.asort.php

Next Article
PHP | asort() Function

J

jit_t
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-array
  • PHP-function

Similar Reads

    PHP arsort() Function
    The arsort() in PHP is used to sort an array according to values. It sorts in a way that relation between indices and values is maintained. By default it sorts in descending order of values. Syntax: bool arsort( $array, $sorting_type ) Parameters: This function accepts two parameters as mentioned ab
    2 min read
    PHP Array Functions
    Arrays are one of the fundamental data structures in PHP. They are widely used to store multiple values in a single variable and can store different types of data, such as strings, integers, and even other arrays. PHP offers a large set of built-in functions to perform various operations on arrays.
    7 min read
    PHP | collator_asort() Function
    The collator_asort() function is an inbuilt function in PHP which is used to sort array maintaining the index association. This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. The array elements are sorted according to curr
    2 min read
    PHP | collator_sort() Function
    The collator_sort() function is an inbuilt function in PHP which is used to sort an array using specified collator. This function returns True on success or False on failure. Syntax: Procedural style: bool collator_sort( $coll, $arr, $sort_flag ) Object oriented style: bool Collator::sort( $arr, $so
    2 min read
    PHP DsSet sort() Function
    The Ds\Set::sort() function of DS\Set class in PHP is used to in-place sort the elements of a specified Set instance according to the values. By default, the Set is sorted according to the increasing order of the values. Syntax: void public Ds\Set::sort ([ callable $comparator ] ) Parameters: This f
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences