0

I have the following sequence of numbers.

$str = '11 10 9 8 7 6 5 4 3 2 1 13 12';

How can I turn the above portion into this.

13 12 11 10 9 8 7 6 5 4 3 2 1

I have been messing around with this.

$str = $numbers; 
$stringParts = str_split($str);
rsort($stringParts);

But the above only returns this when imploded.

11 10 9 8 7 6 5 4 3 2 1 31 21

Here's how I am gathering the numbers.

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;              
$catquery = new WP_Query( 'cat='.$category_id.'&posts_per_page=-1000' ); 
while($catquery->have_posts()) : $catquery->the_post();
$numbers = strip_tags( get_the_term_list( get_the_ID(), 'SECONDID', '', ', ', '' ) ); 


$str = $numbers; 
$stringParts = str_split($str);
rsort($stringParts);
echo implode('', $stringParts);

This is taking the numbers randomly from WordPress custom taxonomy field, and not just placed into a string.

Screenshot edit because people don't like to believe.

enter image description here

Someone
  • 115
  • 6
  • is your array values as int or string? – slon Sep 18 '18 at 13:27
  • Are you sure the numbers are int not string? – Dieter Kräutl Sep 18 '18 at 13:27
  • please refer: https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php – Aman Agarwal Sep 18 '18 at 13:27
  • Well, they are strings, thats what `str_split` gives. But how did you end up with `11` and `13` from str_split anyway? Given the lack of the second parameter, str_split splits into strings of length 1. – Stratadox Sep 18 '18 at 13:30
  • `str_split()` splits your string into letters, you probably want `explode()`, don't you? – Karsten Koop Sep 18 '18 at 13:31
  • 2
    The code you show doesn't produce the output you claim it does: https://3v4l.org/CZBGR Please could you [edit] your question to include a [mcve] - that is, a short piece of code which we can run for ourselves and see the same result you are seeing. – IMSoP Sep 18 '18 at 13:32
  • @IMSoP updated my post – Someone Sep 18 '18 at 13:35
  • 2
    @Someone I sincerely doubt that you get the output `11 10 9 8 7 6 5 4 3 2 1 31 21` with that code. The way you use str_split it can only give single-digit results (0-9) – Stratadox Sep 18 '18 at 13:38
  • 1
    @Someone It's not that we don't believe that's your output, it's that we don't believe it's coming from the input you showed us. That's the point of creating a [mcve]: we shouldn't need to download your entire Wordpress database to reproduce the problem, the example you give should demonstrate the problem. Try for yourself by clicking the link I gave earlier: the code you gave us does not produce that output. – IMSoP Sep 18 '18 at 13:46
  • Why not using `explode` instead of `str_split` ? According to your code, shouldn't `$numbers` look like `13, 12, .... 1` ? – Zyigh Sep 18 '18 at 14:08
  • To help you come up with a better example, try running `var_dump($numbers);` and check what the actual string is. Then take that string, and play around with different ways to manipulate it, completely separate from that piece of Wordpress code. In other words, always break the problem down into smaller parts. – IMSoP Sep 18 '18 at 14:09

3 Answers3

1

This chunk of code will solve your problem:

$str = '11 10 9 8 7 6 5 4 3 2 1 13 12';
$nums_arr = explode(' ',$str);
rsort($nums_arr);
$new_str = implode(" ",$nums_arr);

echo $new_str;
Brice Coustillas
  • 1,837
  • 2
  • 11
  • 16
Harry baldaniya
  • 167
  • 1
  • 11
1

As pointed out in comments, the actual result of the sample you posted would be this:

98765433221111110

That's because str_split splits the string into equal-sized parts, defaulting to 1 character. So the result of str_split('11 10 9 8 7 6 5 4 3 2 1 13 12') is an array of 28 one-character strings. Sorting these will put all the 9s first, then the 8s, and so on, with all the spaces added at the end.

What you actually wanted to do was split the string on space, using the explode function:

$str = $numbers; 
$stringParts = explode($str, ' ');
rsort($stringParts);

This gives you an array of 13 strings, each either one or two characters long. Sorting them then gives you the result you wanted, as PHP correctly guesses that you want them interpreted as numbers.

IMSoP
  • 65,743
  • 7
  • 83
  • 127
-2

Since this is a very short series, use the bubble sort algorithm. It is widely documented, for instance here: https://www.geeksforgeeks.org/bubble-sort/

If you want to change the sort order from ascending to descending, change the > boolean operator to <.

One more thing: if your series makes up a string first convert it to an array: this will make the algorithm in the example easier to understand for ou.

Brice Coustillas
  • 1,837
  • 2
  • 11
  • 16