Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return array containers diversely with explode

So explode functions breaks a string into arrays like this:

$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));

And the result would be:

Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )

But what if we need to sort it diversly, so the expected result would be looked like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Array ( [0] => day [1] => beautiful [2] => a  [3] => It's [4] => world. [5] => Hello  )

So how to do this with explode function?

>Solution :

You can use array_reverse like this for your desired output:

$str = "Hello world. It's a beautiful day.";
var_dump(array_reverse(explode(" ",$str)));

// Output
array(6) {
  [0]=>
  string(4) "day."
  [1]=>
  string(9) "beautiful"
  [2]=>
  string(1) "a"
  [3]=>
  string(4) "It's"
  [4]=>
  string(6) "world."
  [5]=>
  string(5) "Hello"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading