3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Works fine if the key are not numerical */ $arrTags = []; $arrTags['mango'] = 2; $arrTags['orange'] = 4; $arrTags['apple'] = 2; $arrTags['banana'] = 3; array_multisort(array_values($arrTags), SORT_DESC, array_keys($arrTags), SORT_ASC, $arrTags); echo "Elegant solution works fine if keys are not numerical:\r\n"; var_dump($arrTags); /** * If keys are numerical, they will be ignored */ $arrTags = []; $arrTags[10] = 2; $arrTags[20] = 4; $arrTags[30] = 2; $arrTags[40] = 3; array_multisort(array_values($arrTags), SORT_DESC, array_keys($arrTags), SORT_ASC, $arrTags); echo "If keys are numerical, they will be ignored:\r\n"; var_dump($arrTags); /** * A slightly less elegant solution will work. */ $arrTags = []; $arrTags[10] = 2; $arrTags[20] = 4; $arrTags[30] = 2; $arrTags[40] = 3; $keys= array_keys($arrTags); $values= array_values($arrTags); array_multisort($values, SORT_DESC, $keys, SORT_ASC); $arrTags = array_combine($keys, $values); echo "If numerical keys are important, the slightly less elegant solution will work:\r\n"; var_dump($arrTags);
Output for git.master, git.master_jit, rfc.property-hooks
Elegant solution works fine if keys are not numerical: array(4) { ["orange"]=> int(4) ["banana"]=> int(3) ["apple"]=> int(2) ["mango"]=> int(2) } If keys are numerical, they will be ignored: array(4) { [0]=> int(4) [1]=> int(3) [2]=> int(2) [3]=> int(2) } If numerical keys are important, the slightly less elegant solution will work: array(4) { [20]=> int(4) [40]=> int(3) [10]=> int(2) [30]=> int(2) }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
55.46 ms | 402 KiB | 8 Q