lav45 / sort-list
排序项目列表并对子项目递归添加前缀
1.0.1
2015-05-07 15:45 UTC
This package is auto-updated.
Last update: 2024-09-26 02:00:53 UTC
README
排序项目列表并对子项目递归添加前缀
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist lav45/sort-list "*"
或者
"lav45/sort-list": "*"
将以下内容添加到您的 composer.json
文件的 require 部分中。
使用方法
一旦安装了 Yii2 扩展,只需在您的代码中使用它即可:
use lav45\SortList; $data = Category::find() ->select(['id', 'parent_id', 'name' => 'title']) ->asArray() ->all(); print_r($data); /** * Array * ( * [0] => Array * ( * [id] => 1 * [parent_id] => * [title] => Auto * ) * * [1] => Array * ( * [id] => 2 * [parent_id] => 1 * [title] => Car * ) * * [2] => Array * ( * [id] => 3 * [parent_id] => 1 * [title] => Motorcycle * ) * ) */ $sortList = (new SortList($data))->getList(); print_r($sortList); /** * Array * ( * [0] => Array * ( * [id] => 1 * [title] => Auto * ) * * [1] => Array * ( * [id] => 2 * [title] => - Car * ) * * [2] => Array * ( * [id] => 3 * [title] => - Motorcycle * ) * ) */ $dropDownList = ArrayHelper::map($sortList, 'id', 'title'); print_r($sortList); /** * Array * ( * [1] => Auto * [2] => - Car * [3] => - Motorcycle * ) */