Converting a PHP array into a Javascript one -
after looking online answer, couldn't find solution problem: kept getting array length of 1.
my code looks 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 1 huge object, , want achieve like
playerpokemon1 = ["9", "9", "rock slide", "blastoise", "water"]
and on...
i've tried other method
$numarray = count($jspoke1); ($i=0; i<$numarray; i++) { echo "playerpokemon1[$i] = ".jspoke1[$i].";\n"; }
but 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, want javascript array values indexed numerically ["9", "9", "blastoise"]
from question unclear weather want blastoise or pokemon in playerpokemon1
i'll try , give answer work either way. you can try code here.
if $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 either following if want pokemon in playerpokemon1
echo "var playerpokemon1 = ".json_encode($formattedpokemons);
or other thing if want blastoise
echo "var playerpokemon1 = ".json_encode($formattedpokemons[0]);
Comments
Post a Comment