mardi 18 février 2020

How to sort associative array by specific order array keys in PHP/LARAVEL?

I'm initializing an array in my abstract class.

And I want to be sort this associative array in dynamically since the array index position has an effect when I export this as an excel sheet file.

public function setExportDetailsData() {
    $headers['barcode']            = Lang::get('core::label.barcode');
    $headers['stockNo']            = Lang::get('core::label.stock.no');
    $headers['productName']        = Lang::get('core::label.product.name');
    $headers['chineseName']        = Lang::get('core::label.chinese.name');
    $headers['category']           = Lang::get('core::label.category');
    $headers['discount2']          = Lang::get('core::label.discount.2');
    $headers['discount3']          = Lang::get('core::label.discount.3');
    $headers['discount4']          = Lang::get('core::label.discount.4');
    $headers['amount']             = Lang::get('core::label.amount');
    $headers['remarks']            = Lang::get('core::label.remarks');
}

I need to change the order it depending on the page/module sorting.

So, example, the order of sorting for page/module 1:

$sorting = [
    'stockNo',
    'barcode',
    'chineseName',
    'productName',
    'category',
    'discount2',
    'discount3',
    'discount4',
    'amount',
    'remarks'
];

And for other page/module has different page sorting.

I just want to sort this associative array dynamically depending on the page/module.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire