tags:

views:

60

answers:

3

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.

+11  A: 
${"sidecount$side"} = array();

But you're better off using arrays:

$sidecount = array("F" = array(), "B" => array());
$sidecount[$side][$plate] = /* ... */
Artefacto
Agreed with array using! Gives much more flexibility in the end... At least it always happens with my scripts.
Tom
+1. Perfect answer IMO.
sberry2A
ooh, that works perfect. thanks!
Screevo
A: 

I think you need to change

$sidecount{$side}[$plate] = 1;

to

${'sidecount'.$side}[$plate] = 1;

However, I would assume you could use it like this instead.

side = 'F';
$plate = 200;

$sidecount = array();
$sidecount[$side] = array();
$sidecount[$side][$plate] = 1;
print_r($sidecount);
sberry2A
A: 
$_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.

RobertPitt