如何 return 使用 explode 反转数组
How to return reverse array with explode
所以 explode 函数将字符串分解成这样的数组:
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
结果将是:
Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )
但是如果我们需要对数组进行反向排序怎么办,那么预期的结果将如下所示:
Array ( [0] => day [1] => beautiful [2] => a [3] => It's [4] => world. [5] => Hello )
那么如何使用爆炸函数来做到这一点?
您可以像这样使用 array_reverse 以获得您想要的输出:
$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"
}
所以 explode 函数将字符串分解成这样的数组:
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
结果将是:
Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )
但是如果我们需要对数组进行反向排序怎么办,那么预期的结果将如下所示:
Array ( [0] => day [1] => beautiful [2] => a [3] => It's [4] => world. [5] => Hello )
那么如何使用爆炸函数来做到这一点?
您可以像这样使用 array_reverse 以获得您想要的输出:
$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"
}