Screevo
Screevo

Reputation: 75

PHP using a variable in a variable name

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.

Upvotes: 2

Views: 166

Answers (2)

RobertPitt
RobertPitt

Reputation: 57278

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

Upvotes: 0

Artefacto
Artefacto

Reputation: 97845

${"sidecount$side"} = array();

But you're better off using arrays:

$sidecount = array("F" = array(), "B" => array());
$sidecount[$side][$plate] = /* ... */

Upvotes: 11

Related Questions