Jump to content

pętla foreach z sortowaniem


Recommended Posts

Hej, mam taką tablice:

 

Array ( [0] => stdClass Object ( [meta_id] => 495 [term_id] => 6725 [meta_key] => equipment_category_order [meta_value] => 1 ) [1] => stdClass Object ( [meta_id] => 497 [term_id] => 6739 [meta_key] => equipment_category_order [meta_value] => 4 ) [2] => stdClass Object ( [meta_id] => 499 [term_id] => 6736 [meta_key] => equipment_category_order [meta_value] => 2 ) [3] => stdClass Object ( [meta_id] => 501 [term_id] => 6728 [meta_key] => equipment_category_order [meta_value] => 5 ) [4] => stdClass Object ( [meta_id] => 503 [term_id] => 6732 [meta_key] => equipment_category_order [meta_value] => 3 ) )

 

oraz pętle

 

foreach ($orders_cat as $kolejny) { 
    $kolejny->term_id;
}

 

podpowiedzcie plz w jaki sposob posortowac tablice po meta_value?

 

dzieki

Usługi prawne we wszystkich istotnych dziedzinach prawa - kancelaria prawna w Krakowie | Dentysta wawer który daje powody do uśmiechu  | Szukasz opiekunki do dziecka?

 

Link to post
Share on other sites

https://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php

You can do an ascending sort like this:

usort($inventory, function ($item1, $item2) {
    return $item1['price'] <=> $item2['price'];
});

Or a descending sort like this:

usort($inventory, function ($item1, $item2) {
    return $item2['price'] <=> $item1['price'];
});

 

  • Thanks 1

HTTP 200 usługi IT - Dariusz Janicki | Realizacja serwisów www oraz oprogramowania w PHP / C# / Golang / Node.js / MySQL
- Full Stack Developer - :chatownik:

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Terms of Use Privacy Policy