Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP using a variable in a variable name

Tags:

variables

php

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.

like image 987
Screevo Avatar asked Mar 11 '26 13:03

Screevo


1 Answers

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

But you're better off using arrays:

$sidecount = array("F" = array(), "B" => array());
$sidecount[$side][$plate] = /* ... */
like image 75
Artefacto Avatar answered Mar 14 '26 01:03

Artefacto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!