Looping a multidimensional array to update values in PHP

Sam :

in these troubling times I've been trying to keep my social contact up and that includes my board game club.

I figured we could still play "King of Tokyo" as it involves no "secret cards" (ie, nothing in your hands only you can see, everything is on the table). So I set up about five webcams (well, two webcams, two mobile phones and a tablet all running IP Webcam), one for my pretty face, one for the board, one for the dice, one for the player stat cards and a roaming one if needs be. OBS studio worked a charm linking everything together and switching where needs be plus the PIP so you could see board, dice rolls and stat cards all on one screen.

However, that's all a tad complex and I spent focusing so much on managing the cameras I fluffed my role as a GM up a few times so decided to simplify things, I've written a small PHP script that does the dice rolls and player stats so I can link that in as an overlay on OBS and drop two cameras, merging that next to the game board.

So, long intro, apologies, here's the problem - TL;DR join in here:

I have the following array:

$monsters = array(
    array("Alienoid",0,10),
    array("Space Penguin",0,10),
    array("Meka Dragon",0,10),
    array("Cyber Kitty",0,10),
    array("The King",0,10),
    array("Gigazaur",0,10)
    );

Which is basically for the first load (name, points, health), I randomise it to randomise players turns on first load and so from then on I re-read it back in from $_POST data to maintain order:

//names from last round
if (isset($_POST['mon0'])){
    $monsters[0][0] = $_POST['mon0'];
} 
if (isset($_POST['mon1'])){
    $monsters[1][0] = $_POST['mon1'];
} 
if (isset($_POST['mon2'])){
    $monsters[2][0] = $_POST['mon2'];
} 
if (isset($_POST['mon3'])){
    $monsters[3][0] = $_POST['mon3'];
} 
if (isset($_POST['mon4'])){
    $monsters[4][0] = $_POST['mon4'];
} 
if (isset($_POST['mon5'])){
    $monsters[5][0] = $_POST['mon5'];
} 

And that all works perfectly fine. However, I'm getting a bit carried away and want to adjust it from this rather static 6 monsters list to N monsters for N players (game limit is 6, so N can at most be 6, but could be as little as 2).

So I thought I'd just do a for loop like this:

//get names from last round
for ($x = 0; $x < count($monsters); $x++) {
    if (isset($_POST['mon'+$x])){
        $monsters[$x][0] = $_POST['mon'+$x];
    } 
}

Now, as I'm still using my static $monsters list from above count($monsters) is 6 but this doesn't seem to work at all and all my player stats and names disappear. I've seen a lot of for loop tutorials and questions on here and they're all about displaying data, I'm happy with that, but I'm not sure how I dynamically update my array - I'm not a coder, just poke about a bit with PHP in the past and thought it could help solve my problem and keep us all playing and chatting during what is a pretty dull period.

Thanks for reading, big thanks if you can help.

BTW, happy to share the full dodgy code (as in it works, but, could probably be better by someone who knows what they're doing) if anyone else would like to take this online with their friends, using OBS Studio and Virtual Cam plugin you can basically stream the game to any webex you use, we used Google Hangouts which worked averagely (lost one guy at the start, another couldn't work out how to log in) but it does allow you to click on a person and keep their screen as the focus rather than jump around to who's talking. So my plan for the next game is just this window down the side and a webcam pointed at the gameboard as the main pic with a tiny me in the corner.

Maxqueue :

Need to use . instead of +

for ($x = 0; $x < count($monsters); $x++) {
    if (isset($_POST['mon'.$x])){
        $monsters[$x][0] = $_POST['mon'.$x];
    } 
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=398435&siteId=1