I have a object and have a problem, this object here $product have this keys ( product_title, product_price, product_stock ) and more, I need to sort after product_title and if my object have this products in I can't sort it right.
test product 1
test product 11
test product 111
test product 2
test product 20
test product 3
test product 33
test product 333
The right way to sort it its like this:
test product 1
test product 2
test product 3
test product 11
test product 33
test product 20
test product 111
test product 333
I have tried sort
and rsort
without开发者_运维知识库 luck.
If it can help, my object looks like this:
array(8) {
[0]=>
object(stdClass)#12 (49) {
["product_id"]=>
string(3) "335"
["product_guid"]=>
string(36) "F0161D03-33EF-6F40-C816-AEDB33640E36"
["account_id"]=>
string(2) "28"
["category_id"]=>
string(3) "189"
["brand_guid"]=>
NULL
["product_title"]=>
string(10) "Fadestativ"
["push_date"]=>
string(19) "2011-01-18 13:02:11"
["product_price"]=>
string(3) "560"
["product_price_before"]=>
string(1) "0"
["on_stock"]=>
string(1) "0"
["product_weight"]=>
string(1) "0"
["product_int_number"]=>
string(0) ""
["product_ext_number"]=>
string(0) ""
["product_small_info"]=>
string(0) ""
["product_big_info"]=>
string(31) "3 benet fad stativ højde 19 cm"
["active"]=>
string(1) "1"
["meta_title"]=>
string(0) ""
["meta_keyword"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["product_new"]=>
string(1) "0"
["indicative_price"]=>
string(1) "0"
["cost_price"]=>
string(1) "0"
["id"]=>
string(2) "28"
["domain"]=>
string(26) "eventhuset.schemecloud.com"
["password"]=>
string(32) "098f6bcd4621d373cade4e832627b4f6"
["resellerid"]=>
string(1) "2"
["defualt_lang"]=>
string(2) "dk"
["quota"]=>
string(3) "100"
["account_type"]=>
string(3) "cms"
["busniess_name"]=>
string(0) ""
["busniess_street"]=>
string(0) ""
["busniess_city"]=>
string(0) ""
["busniess_zipcode"]=>
string(0) ""
["busniess_contry"]=>
string(0) ""
["busniess_phone"]=>
string(0) ""
["busniess_contact_person"]=>
string(0) ""
["busniess_contact_phone"]=>
string(0) ""
["busniess_email"]=>
string(0) ""
["busniess_cvr"]=>
string(0) ""
["invoice_name"]=>
string(0) ""
["invoice_street"]=>
string(0) ""
["invoice_city"]=>
string(0) ""
["invoice_zipcode"]=>
string(0) ""
["invoice_contry"]=>
string(0) ""
["invoice_contact_person"]=>
string(0) ""
["invoice_cvr"]=>
string(0) ""
["payment_expires"]=>
string(19) "0000-00-00 00:00:00"
["next_order_number"]=>
string(5) "10001"
["order_prefix"]=>
string(0) ""
}
[1]=>
object(stdClass)#14 (49) {
["product_id"]=>
string(3) "306"
["product_guid"]=>
string(36) "8119C253-84C7-73AC-4125-DF4B1728A203"
["account_id"]=>
string(2) "28"
["category_id"]=>
string(3) "189"
["brand_guid"]=>
NULL
["product_title"]=>
string(15) "Kartoffel skål"
["push_date"]=>
string(19) "2011-01-17 13:40:08"
["product_price"]=>
string(3) "680"
["product_price_before"]=>
string(1) "0"
["on_stock"]=>
string(1) "0"
["product_weight"]=>
string(1) "0"
["product_int_number"]=>
string(0) ""
["product_ext_number"]=>
string(0) ""
["product_small_info"]=>
string(0) ""
["product_big_info"]=>
string(31) "Kartoffel skål i rustfri stål"
["active"]=>
string(1) "1"
["meta_title"]=>
string(0) ""
["meta_keyword"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["product_new"]=>
string(1) "0"
["indicative_price"]=>
string(1) "0"
["cost_price"]=>
string(1) "0"
["id"]=>
string(2) "28"
["domain"]=>
string(26) "eventhuset.schemecloud.com"
["password"]=>
string(32) "098f6bcd4621d373cade4e832627b4f6"
["resellerid"]=>
string(1) "2"
["defualt_lang"]=>
string(2) "dk"
["quota"]=>
string(3) "100"
["account_type"]=>
string(3) "cms"
["busniess_name"]=>
string(0) ""
["busniess_street"]=>
string(0) ""
["busniess_city"]=>
string(0) ""
["busniess_zipcode"]=>
string(0) ""
["busniess_contry"]=>
string(0) ""
["busniess_phone"]=>
string(0) ""
["busniess_contact_person"]=>
string(0) ""
["busniess_contact_phone"]=>
string(0) ""
["busniess_email"]=>
string(0) ""
["busniess_cvr"]=>
string(0) ""
["invoice_name"]=>
string(0) ""
["invoice_street"]=>
string(0) ""
["invoice_city"]=>
string(0) ""
["invoice_zipcode"]=>
string(0) ""
["invoice_contry"]=>
string(0) ""
["invoice_contact_person"]=>
string(0) ""
["invoice_cvr"]=>
string(0) ""
["payment_expires"]=>
string(19) "0000-00-00 00:00:00"
["next_order_number"]=>
string(5) "10001"
["order_prefix"]=>
string(0) ""
}
and I need to sort after
array(8) {
[0]=>
object(stdClass)#12 (49) {
["product_title"]=>
string(10) "Fadestativ"
}
Well, assuming that they are objects (which from your description and comments they appear to be), you can use a usort
.
PHP 5.3+:
usort(
$array,
function($a, $b) {
return strnatcmp($a->title, $b->title);
}
);
PHP <= 5.2:
usort(
$array;
create_function('$a, $b', 'return strnatcmp($a->title, $b->title);`)
);
Change ->title
to whatever property you want to sort by. And if you want case insensitivity, use strnatcasecmp
instead of strnatcmp()
...
Try sort() again but you will have to use a second parameter:
sort($array, SORT_STRING);
精彩评论