[英]PHP using a variable in a variable name
I have two preexisting variables: 我有两个预先存在的变量:
$side
(which is either F or B) $side
(F或B)
and 和
$plate
(which is a 3 digit number) $plate
(这是一个3位数字)
I am trying to build two arrays called 我正在尝试构建两个名为的数组
$sidecountF
and $sidecountB
和
$sidecountB
using the following code: 使用以下代码:
$sidecount{$side}[$plate] = 1;
assuming $side is F and $plate is 200, I'm hoping that the end result is that 假设$ side是F而$ plate是200,我希望最终结果是
$sidecountF[200] = 1;
I am, at the beginning, declaring sidecountF
and sidecountB
as arrays with 我在开始时将
sidecountF
和sidecountB
声明为数组
$sidecountF = array();
$sidecountB = array();
So now I'm stumped. 所以现在我很难过。
${"sidecount$side"} = array();
But you're better off using arrays: 但是你最好使用数组:
$sidecount = array("F" = array(), "B" => array());
$sidecount[$side][$plate] = /* ... */
$_blank = array(
'sidecount' . $side => array()
);
extract($_blank);
this would be another way of doing it, it also is not bound to creating 1 variable with ${""}
you can create several variables at once. 这将是另一种方式,它也不一定用
${""}
创建1个变量,你可以一次创建几个变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.