如何在PHP数组中使用变量作为键值?

时间:2015-03-19 19:53:29

标签: php

我想在PHP $par_id = '31512071';函数中使用array();变量。

我尝试在以下php代码中使用"par_id" => $par_id而不是"par_id" => 31512071,但它不起作用。如何在array();方法中使用变量?

$par_id = '31512071';

$params = array(
   "parcelNum" => "315-12-071",
   "b_2" => "Search",
   "opts"=>"2",
   "txtPageSize" => 10,
   "par_id" => 31512071
);

1 个答案:

答案 0 :(得分:2)

你可以这样做:

$par_id = '31512071';

$params = array(
   "parcelNum" => "315-12-071",
   "b_2" => "Search",
   "opts"=>"2",
   "txtPageSize" => 10,
   "par_id" => "{$par_id}"
);