InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP

    PHP strings

    • PHP - split string examples
    • PHP - take last n characters of a string
    • PHP check if string contains something

    PHP Array

    • PHP - associative array value in double quoted string
    • PHP - check if a value is in array
    • PHP - convert array to associative array
    • PHP - print array in one line
    • PHP array foreach - code snippets
    • PHP array map example
    • PHP check if key exists in array
    • PHP sort associative array using custom compare function
    • php get array value with default

    PHP Regex

    • PHP Regex

    PHP Cookbook

    • PHP - assign multi lines string to a variable
    • PHP - call a function with arguments in array examples
    • PHP - get calling file name and line number using debug_backtrace
    • PHP - get class name and file name from an object
    • PHP - get function arguments examples
    • PHP - how to catch errors using set_error_handler
    • PHP - how to initialize static variables
    • PHP - how to use function static variable as cache
    • PHP check if a filename is valid
    • PHP elapsed time
    • PHP execute command and capture shell exit status
    • PHP how to get current url
    • PHP print number to two decimal places
    • PHP remove trailing whitespaces and newline
    • Php equal (==) vs identical (===)
    • Running php eval on code with tags

    PHP functions online

    • PHP functions online
     
    • Home
    • > Tutorials
    • > PHP

    PHP sort associative array using custom compare function

    on Mar 1, 2016

    PHP associative array can be sorted using custom user compare function while maintaining index association using uasort. The function is passed two values and it should return these return values.

    • -1 if value1 should appear before value2
    • 0 if value1 = value2
    • 1 if value1 should appear after value2

    Here is example code where associative array contains employee id as key and age and city in value. This example will sort array by employee age.

    <?php
    // employee id, age, city data
    $arr = array(
      '101' => array(39, "Bangalore"),
      '105' => array(29, "Delhi"),
      '109' => array(35, "Delhi"),
    );
    uasort($arr, function($a, $b) {
      if ($a[0] < $b[0]) {
        return -1;
      } else if ($a[0] == $b[0]) {
        return 0;
      } else {
        return 1;
      }
    });
    print_r($arr);
    ?>
    Array
    (
        [105] => Array
            (
                [0] => 29
                [1] => Delhi
            )
    
        [109] => Array
            (
                [0] => 35
                [1] => Delhi
            )
    
        [101] => Array
            (
                [0] => 39
                [1] => Bangalore
            )
    
    )
    
    Env: PHP 7.4.33 (Linux)

    Suggested posts:

    1. python print examples
    2. PHP – how to initialize static variables
    3. CSS – !important examples
    4. PHP – get function arguments examples
    5. Find authentication methods an ssh server supports
    6. PHP – check if a value is in array
    7. Javascript for-in loop to iterate over keys of javascript object
    8. Curl with download/upload rate limit – code snippets
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged PHP, PHP arrays, PHP cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress