- var_dump: documentation ( source)
- json_decode: documentation ( source)
- usort: documentation ( source)
- ord: documentation ( source)
<?php
$js = '{
"BHS": {
"text": "Beacon Hill School",
"optgroup": "Kowloon"
},
"HSK": {
"text": "ESF International Kindergarten (Hillside)",
"optgroup": "Hong Kong Island"
},
"KJS": {
"text": "Kowloon Junior School",
"optgroup": "Kowloon"
}
}';
$json = json_decode($js);
function sortByText($a, $b) {
return ord($a['text'][0]) - ord($b['text'][0]);
}
usort($json, 'sortByText');
var_dump($json);