0

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. Thanks again

Barmar
  • 741,623
  • 53
  • 500
  • 612
Hathakas
  • 3
  • 1
  • Why are you subtracting 1 from count in your loops? You're skipping the last element of each array. – Barmar Nov 12 '19 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)`? – Barmar Nov 12 '19 at 22:17
  • What is `$tempshipinfo[!i]` supposed to mean? – Barmar Nov 12 '19 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. – Hathakas Nov 12 '19 at 22:19
  • You should probably use `implode()` when combining the strings. – Barmar Nov 12 '19 at 22:25
  • So This is the string that's being exploded into ships "111 Paladin nafenafe 50zzz 112 Xavar nafenafe 150zzz 113 Shino nafenafe 250zzz 114 Poli nafenafe 350zzz" $Ship[0] would be "111 Paladin nafenafe 50" So I tried exploding $Ship[0] to get another array that contains this info, and then I was going to send them into the database. but I don't think exploding an array element works? So now I started looking at multidimensional arrays but can't figure it out lol – Hathakas Nov 12 '19 at 22:27
  • What is the final result you're trying to create? Just a 2-dimensional array of all the stats? – Barmar Nov 12 '19 at 22:32
  • Yeah I'd like to have a Row of ships, with 4 columns. The first ship would for example have an ID of 111, name "Paladin, username "nafenafe" and hp "50" – Hathakas Nov 12 '19 at 22:36

1 Answers1

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you so much!! That's exactly what I've been trying to do for the last 3 days. I'm going to read up on array_map. I used the first example you gave and it works! Thanks again – Hathakas Nov 12 '19 at 22:45