1

I am beginner in Laravel. I write my project in Laravel 5.8. I write code which generate array.

I have this array (result by dd($array)):

array:15 [▼
  0 => array:7 [▼
    "xkey" => 118
    "key" => 0
    "date" => "2020-01-19"
    "id" => 118
    "dishType" => "3"
    "name" => "Mięso z piersi kurczaka, bez skóry"
    "summaryQuantity" => "100"
  ]
  1 => array:7 [▼
    "xkey" => 251
    "key" => 0
    "date" => "2020-01-19"
    "id" => 251
    "dishType" => "3"
    "name" => "Olej rzepakowy"
    "summaryQuantity" => "23"
  ]
]

I need sort my result by "name" ASC.

i try:

ksort($fruits);

But it's not working. How can I make it?

This code help me:

array_multisort(array_column($shoppingArrayTmpData, 'name'), SORT_ASC, $shoppingArrayTmpData);
opolopo
  • 35
  • 4
  • Welcome to Stack Overflow : Does this help you ? https://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php – Altherius Jan 23 '20 at 12:01
  • Hi:) This help me: array_multisort(array_column($shoppingArrayTmpData, 'name'), SORT_ASC, $shoppingArrayTmpData); – opolopo Jan 23 '20 at 12:12
  • Hello @opolopo, if you resolve it you can close it. Try to don't keep open questions were resolved. – Maxi Schvindt Jan 23 '20 at 12:21

2 Answers2

3

It is easy for collection sortBy;

The collect method convert array to collection

The sortBy method sorts the collection by the given key.

And the all method will change collection to array(PS: you can use toarray())

collect($array)->sortBy('name')->all();
TsaiKoga
  • 12,914
  • 2
  • 19
  • 28
1

Try this one

collect($array)->sortBy('name')->all();
Rashed Hasan
  • 3,721
  • 11
  • 40
  • 82
  • "_Try this one_" does a poor job of educating/empowering the OP and future researchers as to how the syntax works and why it is good advice. Please be more generous with your knowledge. – mickmackusa Jan 23 '20 at 13:04