0

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!

nshah
  • 597
  • 2
  • 9
  • 30

1 Answers1

1

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);
Tomas Creemers
  • 2,645
  • 14
  • 15
  • is it supposed to be $obj2->optionid? or optionname – nshah Aug 09 '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 09 '13 at 22:55
  • im getting an "Parse error: syntax error, unexpected T_FUNCTION" on the first line, usort(...) – nshah Aug 09 '13 at 22:58
  • The function just compares the "optionname" field of both objects (records), and returns a value that `sort` can use to order the array items. Thus: `$obj1->optionname` vs. `$obj2->optionname` – bart Aug 09 '13 at 22:59
  • The syntax I use in my solution is only supported in PHP 5.3 and above. Are you using an older PHP version? If so, write the comparison function separate (`function compareOptions($obj1, $obj2) {...}`) and pass it by name (`usort($arr, 'compareOptions');`). – Tomas Creemers Aug 09 '13 at 23:00
  • function compareOptions($obj1, $obj2) { return strcasecmp($obj1->optionname, $obj2->optionname); } usort($searchoptions, compareOptions()); this isn't working either, im sorry, im new to php, if you don't mind can you write it out fully – nshah Aug 09 '13 at 23:02
  • You need to pass the function name to usort as a string: `usort($searchoptions, 'compareOptions');`. I'll update my answer to have a solution for PHP versions < 5.3. – Tomas Creemers Aug 09 '13 at 23:03
  • ok fixed all my errors, but the array is [{"optionname":"wpeogagpoar","optionid":"1030886790"},{"optionname":"genpwaighipwe","optionid":"1193090269"},{"optionname":"iphewajgiewohfoidsahg","optionid":"1385274203"},{"optionname":"fndbwoiaghoe","optionid":"1105065296"},{"optionname":"Robotics","optionid":"80229"},{"optionname":"InventSBHS","optionid":"35741"}] i cannot get it into alphabetical order – nshah Aug 09 '13 at 23:11
  • 1
    Could you please clarify the problems you are still having in your original question? Thanks. – Tomas Creemers Aug 09 '13 at 23:14
  • I want the array to be sorted alphabetically based on all the optionnames, it appears that the array changed but it is still not alphabetical – nshah Aug 09 '13 at 23:16
  • It is working when I test it. You can see for yourself here: http://codepad.org/uuEw2uqP or by copying the example in my answer here: http://sandbox.onlinephpfunctions.com/ . – Tomas Creemers Aug 09 '13 at 23:24
  • wtf why isn't it working for me: http://clubbedin.zymichost.com/search.php this is my server – nshah Aug 09 '13 at 23:29
  • i have the same exact code as you had – nshah Aug 09 '13 at 23:30
  • 1
    I have no way to answer that. – Tomas Creemers Aug 09 '13 at 23:41