PHP code example of lav45 / sort-list
1. Go to this page and download the library: Download lav45/sort-list library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lav45 / sort-list example snippets
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
* )
*/