1, one-dimensional array
Structure:
{
"Data": [
{
"id": 1,
"Name": "Package One",
"category": null,
' Type ': 1,
"Short_introduction": "Package Introduction",
"Detail": "Package Details",
"Price": 4500,
"Discount_price": null
},
{
"id": 2,
"Name": "Package Two",
"category": null,
' Type ': 1,
"Short_introduction": "Package Introduction II",
"Detail": "Package details two",
"Price": 2300,
"Discount_price": null
},
{
"id": 3,
"Name": "Package Three",
"category": null,
' Type ': 1,
"Short_introduction": "Package Three",
"Detail": null,
"Price": 2300,
"Discount_price": null
},
{
"id": 4,
"Name": "Package Four",
"category": null,
' Type ': 1,
"Short_introduction": "Package Four",
"Detail": "Package Details",
"Price": 1700,
"Discount_price": null
}
],
"Meta": {
"Pagination": {
"Total": 4,
"Count": 4,
"Per_page": 10,
"Current_page": 1,
"Total_pages": 1,
"Links": []
}
}
}
$sort = Request ()->input (' SortBy ', ' id ');
$order = Request ()->input (' Order ', ' ASC ');
$paginator = $this->packageservice->with (includes ())->paginate ();
$packages = Fractal ($paginator, New Packagetransformer ())
->parseincludes (includes ())
->toarray ();
$results = [];
if (' price ' = = $sort) {
foreach ($packages [' data '] as $unique => $package) {
$results [] = $package [' Price '];
}
if (' asc ' = = $order) {
Array_multisort ($results, SORT_ASC, $packages [' data ']);
}else{
Array_multisort ($results, Sort_desc, $packages [' data ']);
}
}
if (! $packages) {
Return error (\msg::P ackage_not_found);
}
return $this->response ($packages);
2, two-dimensional array
$arrUsers = array ( array ' id ' => 1, ' name ' => ' John ', ' age ' => 25, ), array ( ' id ' => 2, ' name ' => ' Dick ', ' age ' => 23, ), array ( ' id ' => 3, ' name ' => ' Harry ', ' age ' => 40, ), array ( ' id ' => 4, ' name ' => ' Zhao Liu ', ' age ' => 31, ), array ( ' id ' => 5, ' Name ' => ' Yellow Seven ', ' age ' => 20, ), ; $sort = array ( ' Direction ' => ' sort_desc ', //sort order flag SORT_DESC descending;sort_asc Ascending ' field ' => ' age ', //sort field ; $arrSort = array (); foreach ($arrUsers AS $uniqid => $row) { foreach ($row AS $key => $value) { $arrSort [$key][$ uniqid] = $value; } }