1

I'm super new at php, I'm working on a game in Unity c# and I'm trying to send and receive data from a mysql server. I think I'm having a problem with syntax. I basically have a string that's being sent from my c# script that holds multiple ships with their stats. The ships are seperated by zzz which is a string of it's stats separated by space. I'd like to be able to set the stats array within the ships array. This is what I have but it's not working. Thanks!!

$shiparraytobesplit = $_POST["shipinventory"];
$ships =  explode("zzz", $shiparraytobesplit);

for($i = 0; $i<count($ships)-1; $i++)
{
    
    $tempship[$i] = $ships[$i];
    $tempshipinfo = explode(" ", $tempship);

    for($ii = 0; $ii<count($tempshipinfo[!i])-1; $ii++)
    {
        //$shipinfo[$tempship][] = $info . '_' . $tempshipinfo;
        $shipinfo[$ii] = $tempshipinfo[$ii];    
    }
    
echo $shipinfo[1];
}

I've tried a few variations but I can't seem to get it to work with this example. I'm probably missing something simple since I'm a total noob to php and kind of new to programming.

8
  • Why are you subtracting 1 from count in your loops? You're skipping the last element of each array. Commented Nov 12, 2019 at 22:15
  • explode($" ", $tempship) makes no sense. $tempship is an array, not a string. Did you mean to write $tempship = $ships[$i];? Why not use foreach ($ships as $tempship)? Commented Nov 12, 2019 at 22:17
  • What is $tempshipinfo[!i] supposed to mean? Commented Nov 12, 2019 at 22:18
  • haha I'm not very good with combining strings so I somehow ended up with an extra iteration that only has " " at the end of it. Commented Nov 12, 2019 at 22:19
  • You should probably use implode() when combining the strings. Commented Nov 12, 2019 at 22:25

1 Answer 1

0

You have some extraneous subscripts that aren't needed.

If you have an extra element in the array, it's probably easier to just unset it, then you can loop over the entire array. array_map() is an easy way to produce a new array from an existing array.

$shiparraytobesplit = $_POST["shipinventory"];
$ships =  explode("zzz", $shiparraytobesplit);
unset($ships[count($ships)-1]);

$shipinfo = array_map(function($ship) {
    $tempshipinfo = explode(" ", $ship);
    unset($tempshipinfo[count($tempshipinfo)-1]);
    return $tempshipinfo;
}, $ships);

print_r($shipinfo);

If you want associative arrays, you can do that in the function.

$shiparraytobesplit = $_POST["shipinventory"];
$ships =  explode("zzz", $shiparraytobesplit);
unset($ships[count($ships)-1]);

$shipinfo = array_map(function($ship) {
    $tempshipinfo = explode(" ", $ship);
    $ship_assoc = [
        "id" => $tempshipinfo[0],
        "name" => $tempshipinfo[1],
        "username" => $tempshipinfo[2],
        "hp" => $tempshipinfo[3]
        ];
    return $ship_assoc;
}, $ships);
print_r($shipinfo);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.