简体   繁体   中英

Converting a PHP array into a Javascript one

After looking online for an answer, I couldn't find solution to my problem: I kept getting an array with a length of 1.

My code looks like this:

<?php
    echo "var PlayerPokemon1 = ".$jsPoke1.";\n";
?>

Which returns

PlayerPokemon1 = [{"Number":"9","0":"9","Name":"Rock Slide","1":"Blastoise","Type1":"Water","2":"Water","Type2":"","3":"","HP":"362","4":"362","Attack":"295","5":"295"}]

This is all just one huge object, and what I want to achieve is something like

PlayerPokemon1 = ["9", "9", "Rock Slide", "Blastoise", "Water"]

And so on...

I've tried this other method

$numArray = count($jsPoke1);
for ($i=0; i<$numArray; i++)
{
    echo "PlayerPokemon1[$i] = ".jsPoke1[$i].";\n";
}

But to no avail.

Edit:

print_r ($poke1);

gives this:

Array ( [0] => Array ( [Number] => 9 [0] => 9 [Name] => Rock Slide [1] => Blastoise [Type1] => Water [2] => Water [Type2] => [3] => [HP] => 362 [4] => 362 [Attack] => 295 [5] => 295 [Defense] => 339 [6] => 339 [Speed] => 280 [7] => 280 [Move1] => Aqua Tail [8] => Aqua Tail [Move2] => Ice Beam [9] => Ice Beam [Move3] => Dark Pulse [10] => Dark Pulse [Move4] => Rock Slide [11] => Rock Slide [12] => Aqua Tail [Type] => Rock [13] => Water [Power] => 75 [14] => 90 [15] => Ice Beam [16] => Ice [17] => 90 [18] => Dark Pulse [19] => Dark [20] => 80 [21] => Rock Slide [22] => Rock [23] => 75 ) )

And

$jsPoke1 = json_encode($Poke1);
print_r ($jsPoke1);

[{"Number":"9","0":"9","Name":"Rock Slide","1":"Blastoise","Type1":"Water","2":"Water","Type2":"","3":"","HP":"362","4":"362","Attack":"295","5":"295","Defense":"339","6":"339","Speed":"280","7":"280","Move1":"Aqua Tail","8":"Aqua Tail","Move2":"Ice Beam","9":"Ice Beam","Move3":"Dark Pulse","10":"Dark Pulse","Move4":"Rock Slide","11":"Rock Slide","12":"Aqua Tail","Type":"Rock","13":"Water","Power":"75","14":"90","15":"Ice Beam","16":"Ice","17":"90","18":"Dark Pulse ","19":"Dark","20":"80","21":"Rock Slide","22":"Rock","23":"75"}]

Ultimately, what I want is a Javascript array with the values indexed numerically like ["9", "9", "Blastoise"]

From your question it is unclear weather you want just Blastoise or all the pokemon in PlayerPokemon1 but I'll try and give an answer that would work either way. You can try this code here .

If your $jsPoke1 array looks like

$jsPoke1 = [
  [//Blastoise
    'Number' => 9,
    0 => 9,
    1 => 'Blastoise',
    'Name' => 'Rock Slike',
    ],
  [//Totodile
    'Number' => 5,
    0 => 2,
    1 => 'Totodile',
    'Name' => 'Water Gun',
    ]
  ];

You can do

$formattedPokemons = array_map(function($pokemon){
  return array_values($pokemon);
}, $jsPoke1);

And then either do the following if you want all the pokemon in PlayerPokemon1

echo "var PlayerPokemon1 = ".json_encode($formattedPokemons);

Or this other thing if you just want Blastoise

echo "var PlayerPokemon1 = ".json_encode($formattedPokemons[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM