-1

I have a .json file with some data I'm trying to extract. What I need to know is how I would go about making the below variable dynamic.

$MapData['MapData']['0,0']['type'];

What I need to know is how I would replace the 0,0 with $x,$y variables. I've tried to use variable as a variable name.

$MapString = "MapData['MapData']['".$x.",".$y."']['type']";
$MapStringData = $$MapString;

I was unsuccessful in getting this to work. Is there something I'm missing or not doing right?

3
  • Variable variables is unclear and this question is also unclear. Maybe you could include input and expected output Commented Jan 27, 2019 at 18:12
  • when using a foreach instead of a for you wouldn't have that problem at all! Commented Jan 27, 2019 at 18:13
  • I've updated my question to be a little more simple to understand, hope it helps. Commented Jan 27, 2019 at 20:25

1 Answer 1

0

If you can rely on the last cell having the highest row/column number as the key, then you first can extract the keys for the MapData, and with the last (usingend) element split it using explode() with a ,. This uses list() to assign the two values to the rows and column fields...

$cells = array_keys($MapData['MapData']);
list($MapRows, $MapCols) = explode(",", end($cells));
Sign up to request clarification or add additional context in comments.

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.