Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

For example, say I have a PHP array in this format:

[
{"optionname":"Math","optionid":"35741"},
{"optionname":"Robotics","optionid":"80229"},
{"optionname":"fndbwoiaghoe","optionid":"1105065296"},
{"optionname":"iphewajgiewohfoidsahg","optionid":"1385274203"},
{"optionname":"wpeogagpoar","optionid":"1030886790"},   
{"optionname":"genpwaighipwe","optionid":"1193090269"}
]

How can I sort the array by the value of "optionname" alphabetically?

Thanks!

share|improve this question

marked as duplicate by dev-null-dweller, alfasin, andrewsi, Der Golem, Philipp Wendler Mar 8 '14 at 11:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
ok thanks @dev-null-dweller –  nshah Aug 9 '13 at 22:35

1 Answer 1

up vote 1 down vote accepted

I am assuming, due to your code example, that you have a JSON-encoded array.

You want to sort not on the value but on a specific property of the value. PHP can't know which specific property you want to take into account. You have to give PHP a way to know which object comes in front of another with your own function. Then you can tell PHP to use that function for the sorting comparison using usort().

$arr = json_decode('
[
    {"optionname":"Math","optionid":"35741"},
    {"optionname":"Robotics","optionid":"80229"},
    {"optionname":"fndbwoiaghoe","optionid":"1105065296"},
    {"optionname":"iphewajgiewohfoidsahg","optionid":"1385274203"},
    {"optionname":"wpeogagpoar","optionid":"1030886790"},   
    {"optionname":"genpwaighipwe","optionid":"1193090269"}
]
');

usort($arr, function ($obj1, $obj2) {
    return strcasecmp($obj1->optionname, $obj2->optionname);
});

$arr = json_encode($arr);

Note that the code above compares the optionname property case insensitive. If you want PHP to take case into account, replace strcasecmp with strcmp.

Edit: If you are using a PHP version older than 5.3, anonymous functions (like the one used as the second parameter to the usort() function above) are not yet supported. The version below should work then.

$arr = json_decode('
[
    {"optionname":"Math","optionid":"35741"},
    {"optionname":"Robotics","optionid":"80229"},
    {"optionname":"fndbwoiaghoe","optionid":"1105065296"},
    {"optionname":"iphewajgiewohfoidsahg","optionid":"1385274203"},
    {"optionname":"wpeogagpoar","optionid":"1030886790"},   
    {"optionname":"genpwaighipwe","optionid":"1193090269"}
]
');

function compareObjects($obj1, $obj2)
{
    return strcasecmp($obj1->optionname, $obj2->optionname);
}
usort($arr, 'compareObjects');

$arr = json_encode($arr);
share|improve this answer
    
is it supposed to be $obj2->optionid? or optionname –  nshah Aug 9 '13 at 22:52
    
Your question is "How can I sort the array by the value of "optionname" alphabetically?". Then the sorting function needs to compare the optionname of the two given objects to compare. Did you want to compare on both optionname AND optionid, like @dev-null-dweller suggested? –  Tomas Creemers Aug 9 '13 at 22:55
    
no, just optionname –  nshah Aug 9 '13 at 22:57
1  
Could you please clarify the problems you are still having in your original question? Thanks. –  Tomas Creemers Aug 9 '13 at 23:14
1  
I have no way to answer that. –  Tomas Creemers Aug 9 '13 at 23:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.