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);
3
  • Welcome to Stack Overflow : Does this help you ? stackoverflow.com/questions/1597736/… Commented Jan 23, 2020 at 12:01
  • Hi:) This help me: array_multisort(array_column($shoppingArrayTmpData, 'name'), SORT_ASC, $shoppingArrayTmpData); Commented Jan 23, 2020 at 12:12
  • Hello @opolopo, if you resolve it you can close it. Try to don't keep open questions were resolved. Commented Jan 23, 2020 at 12:21

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

@opolopo I change to asc
Now it will work, you need to generate a collection and then you can sort with collections functions.
1

Try this one

collect($array)->sortBy('name')->all();

1 Comment

"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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.