开发者

PHP using a variable in a variable name

开发者 https://www.devze.com 2023-01-12 12:10 出处:网络
I have two preexisting variables: $side (which is either F or B) and $plate (which is a 3 digit number) I am trying to build two arrays called

I have two preexisting variables:

$side (which is either F or B)

and

$plate (which is a 3 digit number)

I am trying to build two arrays called

$sidecountF

and $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

$sidecountF[200] = 1;

I am, at the beginning, declaring sidecountF and sidecountB as arrays with

$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.

0

精彩评论

暂无评论...
验证码 换一张
取 消