I have this string
$s = "red2 blue5 black4 green1 gold3";
I need to order by the number, but can show the numbers.
Numbers will always appears at the end of the word.
the result should be like:
$s = "green red gold black blue";
Thanks!
I have this string
$s = "red2 blue5 black4 green1 gold3";
I need to order by the number, but can show the numbers.
Numbers will always appears at the end of the word.
the result should be like:
$s = "green red gold black blue";
Thanks!
2
Answers
Use one of them, but also try to understand whats going on here.
Does it always follow this pattern – separated by spaces?
I would break down the problem as such:
I would first start with parsing the string into an array where the key is the number and the value is the word. You can achieve this with a combination of
preg_match_all
andarray_combine
Then you could use
ksort
in order to sort by the keys we set with the previous step.Finally, if you wish to return your result as a string, you could
implode
the resulting array, separating by spaces again.An example solution could then be:
The regex I’m using here matches two seperate groups (indicated by the brackets):
a-z
(or capitalised). Its worth noting this only works on the latin alphabet; it won’t match ö, for example.The results of these matches are stored in
$splitResults
, which will be an array of 3 elements:[0]
A combined list of all the matches.[1]
A list of all the matches of group 1.[2]
A list of all the matches of group 2.We use
array_combine
to then combine these into a single associative array. We wish for group 2 to act as the ‘key’ and group 1 to act as the ‘value’.Finally, we sort by the key, and then implode it back into a string.