php - Group Array Items with similar strings -
i have simple array of strings:
$hand = array("05h", "02h", "03c", "02s", "04h");
here's want it:
if number appears more once, group number @ beginning of array. end result be:
$hand = array("02h", "02s", "05h", "03c", "04h");
here's i've pieced far other answers, don't know how make pieces work accomplish goal.
$hand = array("05h", "02h", "03c", "02s", "04h"); rsort($hand,sort_numeric); print_r ($hand); echo "<br>"; function myfunction($card) { return(substr($card,0,2)); } $countedhand = array_count_values(array_map("myfunction",$hand)); asort($countedhand); print_r(array_reverse($countedhand));
uchiha's comment solved original array, have couple more complex arrays doesn't work for. here's couple other arrays.
$hand2 = array("05h", "02h", "03c", "02s", "03h"); $hand3 = array("03s", "02h", "03c", "02s", "03h");
you can use asort() result:
<?php $hand = array("05h", "02h", "03c", "02s", "04h"); asort($hand); print_r($hand); ?>
hope help
Comments
Post a Comment