小编Zai*_*lek的帖子

使用赋值左侧的展开运算符来解构数组,以收集单个变量中的所有剩余元素

我想在 php 中执行解构,就像下面的 javascript 代码一样:

[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a,b,rest);
Run Code Online (Sandbox Code Playgroud)

输出:

10 20 [ 30, 40, 50 ]
Run Code Online (Sandbox Code Playgroud)

我如何在 php 中执行该操作?

我的 PHP 代码是:

<?php
$array = [10, 20, 30, 40, 50]; 

// Using the list syntax:
//list($a, $b, $c[]) = $array;

// Or the shorthand syntax:
[$a, $b, $c[]] = $array;

echo "$a<br>$b<br>";
print_r ($c);
?>
Run Code Online (Sandbox Code Playgroud)

哪个打印:

10
20
Array ( [0] => 30 )
Run Code Online (Sandbox Code Playgroud)

但我想要 $c 中的“[ 30, 40, 50 ]”

php arrays destructuring variable-declaration spread-syntax

5
推荐指数
1
解决办法
992
查看次数