- Articles
- [7z] Extract a 7z archive on Mac with 7z
- [7z] Install the 7z command on macOS with Homebrew
- [awk] Calculate an average
- [awk] Sum numbers from standard input
- [AWS CLI] Add multiple items to a DynamoDB table in one batch
- [AWS CLI] Check which AWS identity the CLI is using
- [AWS CLI] Generate a presigned URL for an S3 object
- [AWS CLI] Put a DynamoDB item from a JSON file
- [AWS CLI] Put an item in a DynamoDB table
- [AWS CLI] Resolve an Invalid base64 error when running aws lambda invoke
- [AWS CLI] Show only file names with aws s3 ls
- [AWS SAM] Install the AWS SAM CLI on macOS with Homebrew
- [AWS SAM] Read a value from Secrets Manager in a template
- [AWS SAM] Skip the change set confirmation during deployment
- [Bash] Check for a regular file with -f
- [Bash] Check whether a directory exists with -d
- [Bash] Generate a UUID
- [Bash] Get the number of elements in an array
- [Bash] Loop over array elements (foreach)
- [Bash] Store the output of ls in an array
- [Bash] Test for a non-empty string with if -n
- [Bash] Write multiple lines to a file with a here document
- [BigQuery] Convert a date-time string to a TIMESTAMP
- [BigQuery] Format Unix seconds as a date and time
- [gawk] Extract text matched by a regular expression capture group
- [Git] Apply a patch created with git diff
- [Git] Cherry-pick a range of commits
- [Git] Combine search patterns with AND in git grep
- [Git] Find the root directory of the working tree
- [Git] Get the current commit hash
- [Git] List branches
- [Git] List files on a specific branch
- [Git] Print only the names of files that match git grep
- [Git] Rebase a branch onto another with git rebase
- [Git] Reset the current branch to match a remote branch
- [Git] Search a specific revision with git grep
- [Git] Show line numbers with git grep
- [Git] Show stash diffs with git stash show -p
- [Git] Switch branches with git switch
- [Git] Undo a commit with git revert
- [GitHub] Resize an image in an issue or pull request
- [Go] Add calendar days to a date
- [Go] Add or subtract seconds from a time.Time value
- [Go] Assert that a slice contains a value with Testify
- [Go] Assign to a nested map without a nil map panic
- [Go] Check an expected value with Testify's assert.Equal
- [Go] Check how many times a mock method was called with Testify
- [Go] Check slice elements in any order with assert
- [Go] Check whether a string ends with a specific suffix
- [Go] Check whether a time.Time value is zero
- [Go] Concatenate two slices with append
- [Go] Convert a uint32 to a string
- [Go] Convert an int to an int64
- [Go] Convert an int64 to a string
- [Go] Generate a presigned URL for an S3 object with aws-sdk-go
- [Go] Get capture groups from every regular expression match
- [Go] Get the day of the week
- [Go] Parse a date-time string into time.Time
- [Go] Pass parameters to a MySQL query with database/sql
- [Go] Prepend elements to a slice
- [Go] Read a command-line flag with the flag package
- [Go] Read a CSV file
- [Go] Read an S3 object with the AWS SDK for Go v2
- [Go] Run one test by name with go test -run
- [Go] Send a POST request with a JSON body
- [Go] Send a SigV4-signed HTTP request to API Gateway with aws-sdk-go-v2
- [Go] Send an HTTP request with Basic authentication
- [Go] Set a mock method's return value with GoMock
- [Go] Show output from passing tests with go test -v
- [Go] Sort a slice of strings alphabetically
- [Go] Upload a string as a text file to S3
- [Go] Use a struct as a map key
- [Go] Wrap a string in an io.ReadCloser
- [Go] Write CSV records to a string
- [Go] Write records to a CSV file
- [Google Apps Script] Append a row to a Google Sheet
- [Google Apps Script] Build a line chart from an array
- [Google Apps Script] Create an event in Google Calendar
- [Google Apps Script] Download an image and save it to Google Drive
- [Google Apps Script] Encode a string in Base64
- [Google Apps Script] Parse a JSON string
- [Google Apps Script] Parse a TSV string
- [Google Apps Script] Send a POST request with a JSON body
- [Google Apps Script] Upload an image to a Slack channel
- [Google Sheets] Convert a Unix timestamp to a date and time
- [Homebrew] List installed formulae
- [JavaScript] Get a Unix timestamp in seconds
- [JavaScript] Parse an integer from a string with parseInt()
- [jq] Convert a string to a number
- [jq] Convert JSON to CSV with @csv
- [jq] Count the elements of an array
- [jq] Exclude objects whose property contains a given string
- [jq] Output strings as plain text
- [jq] Parse a CSV file and convert it to JSON
- [jq] Select objects whose field value starts with a given string
- [jq] Select objects whose property contains a given string
- [MySQL] Check the system and session time zones
- [MySQL] Convert a date and time between time zones
- [MySQL] Convert a date-time string to a Unix timestamp
- [MySQL] Convert a Unix timestamp to a date and time
- [MySQL] Convert a VARBINARY column to a string
- [MySQL] Get the current connection ID
- [MySQL] Get the last day of a month
- [MySQL] Get the number of days in a given month and year
- [Node.js] Check whether a file or directory exists
- [Node.js] Create a directory and its subdirectory together
- [Node.js] Delete a directory synchronously
- [Node.js] Delete a single S3 object
- [Node.js] Detect a missing object error from AWS.S3.getObject (AWS SDK for JavaScript v2)
- [Node.js] Generate a version 4 UUID
- [Node.js] Invoke Lambda with AWS SDK for JavaScript v3
- [Node.js] Send a JSON POST request with Axios
- [npm] Install a specific version of a package
- [osascript] Show a macOS notification from the command line
- [PHP] Encode an array or object as a JSON string
- [PHP] Format a Unix timestamp as a date string
- [PHP] Read the raw HTTP request body
- [PHP] Remove line breaks from a string
- [PHP] Send a POST request with a JSON body
- [PHP] Sort a nested array by one field with usort
- [PHP] Sort an array with usort
- [PHP] Use strict comparison with in_array()
- [Python] Check the exact type of an object
- [Python] Decode percent-encoded text with urllib.parse.unquote
- [Python] Extract a regular expression capture group
- [Python] Generate a presigned URL for an S3 object with Boto3
- [Python] Percent-encode text with urllib.parse.quote
- [Python] Read command-line arguments with sys.argv
- [Python] Send an HTTP request with Basic authentication
- [Ruby] Display a DateTime with a different UTC offset
- [sed] Extract text captured by a regular expression
- [Shell command] Align simple CSV columns in the terminal
- [Shell command] Append a comma to each line with sed
- [Shell command] Calculate a SHA-256 hash with sha256sum
- [Shell command] Check whether two lists contain the same lines
- [Shell command] Convert text to lowercase with awk
- [Shell command] Convert uppercase letters to lowercase with tr
- [Shell command] Create a password-protected ZIP file with zip
- [Shell command] Encode and decode Base64 text
- [Shell command] Expand environment variables with envsubst
- [Shell command] Expand only specified environment variables with envsubst
- [Shell command] Prepend text to a file's contents with echo and cat
- [Shell command] Print line breaks with echo -e in Bash
- [Shell command] Read search patterns from a file with grep
- [Shell command] Remove double quotes from a string
- [Shell command] Remove the first N characters from every line
- [Shell command] Send a POST request with curl
- [Shell command] Show response headers with curl
- [Shell command] Skip the xargs command when there is no input
- [Shell command] Upload a file with curl -F
- [Slack] Add a line break to a /remind command
- [Terraform] Print output values as JSON
- [Terraform] Skip the yes prompt when running terraform apply
- [TypeScript] Fix 'Cannot find name console' for console.log
- [TypeScript] Remove a property from a type with Omit
- [TypeScript] Treat an empty object as an interface type
- [Ubuntu] List installed fonts
- [vim-go] Add Go imports automatically on save
- [Vim] Show tabs and line endings with :set list
- [VS Code] Open a diff from the command line
[PHP] Sort a nested array by one field with usort
Created on November 30, 2026
You can use usort() to sort an array of records by one of their fields. In this example, each record contains a name and age, and the comparison orders the records by age from youngest to oldest.
<?php
$users = [
['name' => 'Tom', 'age' => 20],
['name' => 'Bob', 'age' => 10],
['name' => 'Alice', 'age' => 40],
['name' => 'James', 'age' => 30],
];
usort($users, function ($a, $b) {
return $a['age'] <=> $b['age'];
});
var_dump($users);
The <=> operator returns a negative number, zero, or a positive number according to how the first age compares to the second. To sort from oldest to youngest, swap the operands and return $b['age'] <=> $a['age'].
The ascending sort prints:
array(4) {
[0]=>
array(2) {
["name"]=>
string(3) "Bob"
["age"]=>
int(10)
}
[1]=>
array(2) {
["name"]=>
string(3) "Tom"
["age"]=>
int(20)
}
[2]=>
array(2) {
["name"]=>
string(5) "James"
["age"]=>
int(30)
}
[3]=>
array(2) {
["name"]=>
string(5) "Alice"
["age"]=>
int(40)
}
}