使用PHP / Javascript从Array或JSON中提取数据

时间:2021-08-27 09:52:56

I have an Array and a JSON Object present on my page, both house the same information but are just different types. The array looks like this:

我的页面上有一个数组和一个JSON对象,两者都包含相同的信息,但只是不同的类型。该数组看起来像这样:

Array
(
    [Player One] => Array
        (
            [avg] => 400
            [gp] => 2
            [gs] => 2
            [ab] => 5
            [r] => 0
            [h] => 2
        )
    [Player Two] => Array
        (
            [avg] => 0
            [gp] => 2
            [gs] => 0
            [ab] => 3
            [r] => 0
            [h] => 0
        )
)

The JSON Object looks like this:

JSON对象如下所示:

{
"Player One":{"avg":"400","gp":"2","gs":"2","ab":"5","r":"0","h":"2"},
"Player Two":{"avg":"0","gp":"2","gs":"0","ab":"3","r":"0","h":"0"}
}

In actuality there are 17 players, what I want to do is pull their stats on a mouse hover into a dialog box that will appear. So when the mouse hovers over a players names, the JSON/Array is queried and their stats are displayed - something like this:

实际上有17个玩家,我想做的是将鼠标上的统计数据悬停在一个将出现的对话框中。因此,当鼠标悬停在玩家名称上时,将查询JSON / Array并显示其统计信息 - 如下所示:

$player = $(".hoveredItem").text();
echo("AVG:" . $player['avg'] . "<br>")
echo("GP:" . $player['gp'] . "<br>") 

And so forth.

等等。

Really I'd just be content with finding the way to search for/print data from either or both of these data sets. I can handle pulling the data based on a mouse hover after that.

真的,我只是满足于找到从这些数据集中的一个或两个搜索/打印数据的方法。我可以在此之后处理基于鼠标悬停的数据。

How can I access this data using Javascript or PHP?

如何使用Javascript或PHP访问此数据?


I have been able to access using both JavaScript and PHP, but now I am running into an issue with the space in "Player One" - I keep getting an error 'Unexpected String' when I do:

我已经能够使用JavaScript和PHP访问,但现在我遇到了“Player One”中的空间问题 - 当我这样做时,我不断收到错误'Unexpected String':

alert(json.'Player One'['avg']);

trying to turn 'Player One' into a variable such as $playerone also did not lead to a defined result.

试图将“玩家一号”变成诸如$ playerone之类的变量也没有导致定义的结果。

1 个解决方案

#1


0  

You cannot use json.Player One because there is a space in key, I prefer you to use json['Player One'] this will give you player one stats

你不能使用json.Player One,因为键中有空格,我更喜欢你使用json ['Player One']这会给你玩家一个统计数据

json['Player One'] ///player one stats
json['Player One'].avg // for further details

 var json = {
    			"Player One":{"avg":"400","gp":"2","gs":"2","ab":"5","r":"0","h":"2"},
    			"Player Two":{"avg":"0","gp":"2","gs":"0","ab":"3","r":"0","h":"0"}
    			};
console.log('Player One',json['Player One']);

console.log('Player One Avg : ',json['Player One'].avg);

#1


0  

You cannot use json.Player One because there is a space in key, I prefer you to use json['Player One'] this will give you player one stats

你不能使用json.Player One,因为键中有空格,我更喜欢你使用json ['Player One']这会给你玩家一个统计数据

json['Player One'] ///player one stats
json['Player One'].avg // for further details

 var json = {
    			"Player One":{"avg":"400","gp":"2","gs":"2","ab":"5","r":"0","h":"2"},
    			"Player Two":{"avg":"0","gp":"2","gs":"0","ab":"3","r":"0","h":"0"}
    			};
console.log('Player One',json['Player One']);

console.log('Player One Avg : ',json['Player One'].avg);