theopeneyes / sort-associative-array
一个用于排序关联数组的 Laravel 扩展包
1.0.0
2021-08-04 10:46 UTC
README
这是一个用于排序关联数组的 Laravel 扩展包。
安装
要开始使用,请将 require
安装到您的项目中。
- 通过 Composer
composer require theopeneyes/sort-associative-array
- 通过 composer.json 文件
在您的项目 composer.json
文件的 require
部分中输入以下内容。
"theopeneyes/sort-associative-array": "dev-main",
运行 composer update
以下载扩展包。
composer update
基本用法
假设我们有一个如下所示的巨大数组,并且我们需要根据数组中的某个值对其进行排序
$student = array( array( 'name' => array( 'first_name' => 'Joann', 'last_name' => 'Daly' ), 'age' => 18, 'grade' => 12, 'birth_month' => 5 ), array( 'name' => array( 'first_name' => 'Jaiden', 'last_name' => 'Wolf' ), 'age' => 15, 'grade' => 10, 'birth_month' => 8 ), array( 'name' => array( 'first_name' => 'Lara', 'last_name' => 'Flores' ), 'age' => 8, 'grade' => 3, 'birth_month' => 2 ) );
- 假设按 'age' 排序
use TheOpenEyes\SortAssociativeArray\SortAssociativeArray; SortAssociativeArray::column($student, 'age', SORT_DESC); /* Parameters of 'column' method 1. Array - The array you would like to sort by. 2. Column name 3. Sort Direction - Should be either SORT_ASC or SORT_DESC (Optional, Default is SORT_ASC) */
- 假设按 'first_name' 排序
use TheOpenEyes\SortAssociativeArray\SortAssociativeArray; SortAssociativeArray::columnWithChild($student, 'name', 'first_name', SORT_DESC); /* Parameters of 'columnWithChild' method 1. Array - The array you would like to sort by. 2. Parent column name 3. Child column name 4. Sort Direction - Should be either SORT_ASC or SORT_DESC (Optional, Default is SORT_ASC) */