简体   繁体   English

将PHP数组转换为Javascript数组

[英]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. 在网上寻找答案后,我找不到解决问题的方法:我一直得到长度为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"] 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"] 最终,我想要的是一个Javascript数组,其值在数字上像[“ 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. 从您的问题来看,天气不清楚,您只需要Blastoise还是PlayerPokemon1所有口袋妖怪,但我会尝试给出一个可行的答案。 You can try this code here . 您可以在此处尝试此代码

If your $jsPoke1 array looks like 如果您的$jsPoke1数组看起来像

$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 如果要在PlayerPokemon1所有口袋妖怪,请执行以下操作

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

Or this other thing if you just want Blastoise 或者这另一件事,如果您只想要Blastoise

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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