如何将数组分配给php中的另一个变量?

时间:2012-02-12 16:35:32

标签: php arrays variable-assignment

我在php中定义了一个数组

    $letters = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","W","V","X","Y","Z");

然后我尝试将数组分配给一个新变量,也就是一个数组:

   $new_array = array();
   $new_array =  $letters;

但它不起作用,为什么?

4 个答案:

答案 0 :(得分:8)

你可以做到

$new_array = $letters;

您不必指定类型,因为它是隐含的。

答案 1 :(得分:3)

您无需创建空数组。你的第二个任务实际上是正确的好处是使用var_dump()转储变量的内容以查看其实际值。

试试这个:

$letters = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","W","V","X","Y","Z");
$new_array = $letters;
var_dump($new_array);

答案 2 :(得分:0)

Josh提到范围,这可能是问题所在。如果$letters是全局的并且$new_array的赋值发生在函数中,那么$letters必须在使用前在函数中声明为全局。

答案 3 :(得分:0)

使用克隆关键字

var2 = clone var1;

分享是关怀。帮助他人了解你的知识:)。