0

We are working on a Monopoly game in PHP as a class project and none of us cab figure this out. We need to create and use a multidimentional array for the properties in the game. Below is an example of what this looks like.

$arr2=array();
$prop=array(1,3,6);
$cost=array(60,60,100);

$stuff=$prop[0];
$arr2[$stuff][9]=$cost[0];
echo"$stuff --- $arr2[$stuff][9]"; //(this is line 64)

When we try to run this, we get this output.

Notice: Array to string conversation in ... line 64
1 --- Array[9]

Why is it giving us "Array[9]" instead of 60? Thank you for your time.

1
  • echo "{$stuff} --- {$arr2[$stuff][9]}"; try this Commented May 23, 2016 at 18:11

3 Answers 3

3

Complex array and object expressions need to be wrapped in curly braces {}:

echo "$stuff --- {$arr2[$stuff][9]}";

Or break out of the quotes and concatenate:

echo "$stuff --- " . $arr2[$stuff][9];
//or
echo $stuff . " --- " . $arr2[$stuff][9];

See Variable Parsing - Complex (curly) Syntax.

Sign up to request clarification or add additional context in comments.

Comments

1

echo a PHP variable has its own convention, you can echo a variable as string but sometime it will not work perfectly, So you need to use . concatenation of the variable with string.

echo $stuff." --- ".$arr2[$stuff][9]; //1 --- 60

If you want to use variables inside echo then must use {}.

echo "{$stuff} --- {$arr2[$stuff][9]}"; //1 --- 60

1 Comment

Why should the OP "try something like this"? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
0

Simply change this part

echo"$stuff --- $arr2[$stuff][9]"; //(this is line 64)

with this

echo $stuff." --- ".$arr2[$stuff][9]; //(this is line 64)

You can check it out in this Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.