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:
Array ( [0] => day [1] => beautiful [2] => a [3] => It's [4] => world. [5] => Hello )
So how to do this with explode function?
CodePudding user response:
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"
}