使用php获取json密钥的索引

时间:2021-02-20 21:34:56

I want to find index of key from json array similar to this question Get index of a key in json but i need the solution using php.

我想从json数组中找到键的索引,类似于这个问题在json中获取键的索引,但我需要使用php的解决方案。

here is my json (partial data)

这是我的json(部分数据)

{   
"currentOver":{
    "events":[]
},  
"matchString":"",
"currentPlayer":5, 
"previousOvers":[], 
"innings":[],   
"scorecards":[
    {
        "batting":{
            "players":[
                        {"id":16447,"name":"Rahul Roy"},
                        {"id":12633,"name":"Sijal Thomas"},
                        {"id":16446,"name":"Mohammed Reza"},
                        {"id":16509,"name":"Asif Khan"},
                        {"id":12633,"name":"Koyel Dijesh"},
                        {"id":16468,"name":"Shahrook"},
                        {"id":64691,"name":"Shafiq"},
                        {"id":6518,"name":"Ubaidulah"}
            ]
        }
    }
]

}

and php

foreach ($read_json->scorecards->batting->players as $batsmen => $val) {    
                if($val == 5) {   // if batsman index is 5 then display his name
                    $name = $batsmen->name;

                    echo "<div>$name</div>\n"; 

                }               
}

Please help me to solve this issue.Thanks in advance.

请帮我解决这个问题。谢谢。

7 个解决方案

#1


7  

I think this would suffice your requirement

我认为这足以满足您的要求

http://codepad.org/XQDCKAsB

Find the code sample below as well.

找到下面的代码示例。

       $json  = '{"currentOver":{"events": []},"matchString":"","currentPlayer":5,"previousOvers":[],"innings":[],"scorecards":[{"batting":{"players":[{"id":16447,"name":"Rahul Roy"},{"id":12633,"name":"Sijal Thomas"},{"id":16446,"name":"Mohammed Reza"},{"id":16509,"name":"Asif Khan"},{"id":12633,"name":"Koyel Dijesh"},{"id":16468,"name":"Shahrook"},{"id":64691,"name":"Shafiq"},{"id":6518,"name":"Ubaidulah"}]}}]}';

       $arr = json_decode($json);

       echo '<pre>';

       $currentPlayer = $arr->currentPlayer;

       echo $arr->scorecards[0]->batting->players[$currentPlayer-1]->name; 

#2


3  

Try this code.

试试这个代码。

$info = json_decode('json string');
$currentPlayer = $info->currentPlayer;
$batsman = $info->scorecards[0]->batting->players[$currentPlayer];
echo "<div>{$batsman->name}</div>\n";

Also, note that arrays in PHP are zero-based. If currentPlayer index in json data is based on 1 (rare case, but it exists sometimes) you will ned to use

另请注意,PHP中的数组是从零开始的。如果json数据中的currentPlayer索引基于1(极少数情况,但有时存在),您将需要使用

$batsman = $info->scorecards[0]->batting->players[$currentPlayer - 1];

to get right item from array.

从数组中获取正确的项目。

#3


3  

If you just want to fix your foreach

如果你只想修复你的foreach

  1. $read_json->scorecards->batting should become $read_json->scorecards[0]->batting

    $ read_json->记分卡 - >击球应成为$ read_json->记分卡[0] - >击球

  2. if($val == 5) should become if($batsmen == 5)

    if($ val == 5)if if($ batsmen == 5)

  3. $name = $batsmen->name; should become $name = $val->name;

    $ name = $ batsmen-> name;应该成为$ name = $ val-> name;

#4


2  

You need to use json_decode and array_keys for the array keys from the json.

您需要将json_decode和array_keys用于json中的数组键。

$json = '{ "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" }';
$result = json_decode ($json, true);
$keys = array_keys($result);
print_r($keys); //Array ( [0] => key1 [1] => key2 [2] => key3 )

#5


2  

In Php, You can use the code below, if you wan to fix it using foreach loop

在Php中,如果您想使用foreach循环修复它,可以使用下面的代码

foreach($json->entries as $row)

{

    foreach($row as $key => $val)
    {
        echo $key . ': ' . $val;
        echo '<br>';
    }

}

#6


1  

please have a look following code

请看下面的代码

$json=json_decode($data)->scorecards[0]->batting->players;
foreach ($json as $key => $value) {
  if($key==5){
    echo $value->name ;
  }
}

#7


1  

You can do it using converting JSON string to Array

您可以使用将JSON字符串转换为Array来实现

$json_str = '{   
"currentOver":{
    "events":[]
},  
"matchString":"",
"currentPlayer":5, 
"previousOvers":[], 
"innings":[],   
"scorecards":[
    {
        "batting":{
            "players":[
                        {"id":16447,"name":"Rahul Roy"},
                        {"id":12633,"name":"Sijal Thomas"},
                        {"id":16446,"name":"Mohammed Reza"},
                        {"id":16509,"name":"Asif Khan"},
                        {"id":12633,"name":"Koyel Dijesh"},
                        {"id":16468,"name":"Shahrook"},
                        {"id":64691,"name":"Shafiq"},
                        {"id":6518,"name":"Ubaidulah"}
            ]
        }
    }
]

}';

Decode your JSON string using "json_decode" and you get array

使用“json_decode”解码您的JSON字符串并获得数组

$json_decode_str = json_decode($json_str, true);

Now using foreach you can do anything

现在使用foreach你可以做任何事情

if($json_decode_str['scorecards']){

    foreach($json_decode_str['scorecards'] as $scorecard){

        $player_index = 1;
        if($scorecard['batting']['players']){

            foreach($scorecard['batting']['players'] as $player){

                if($player_index == 5){
                    echo $player['name'];
                }

                $player_index++;
            }

        }

    }

}

Maybe this one help you :)

也许这个帮你:)

#1


7  

I think this would suffice your requirement

我认为这足以满足您的要求

http://codepad.org/XQDCKAsB

Find the code sample below as well.

找到下面的代码示例。

       $json  = '{"currentOver":{"events": []},"matchString":"","currentPlayer":5,"previousOvers":[],"innings":[],"scorecards":[{"batting":{"players":[{"id":16447,"name":"Rahul Roy"},{"id":12633,"name":"Sijal Thomas"},{"id":16446,"name":"Mohammed Reza"},{"id":16509,"name":"Asif Khan"},{"id":12633,"name":"Koyel Dijesh"},{"id":16468,"name":"Shahrook"},{"id":64691,"name":"Shafiq"},{"id":6518,"name":"Ubaidulah"}]}}]}';

       $arr = json_decode($json);

       echo '<pre>';

       $currentPlayer = $arr->currentPlayer;

       echo $arr->scorecards[0]->batting->players[$currentPlayer-1]->name; 

#2


3  

Try this code.

试试这个代码。

$info = json_decode('json string');
$currentPlayer = $info->currentPlayer;
$batsman = $info->scorecards[0]->batting->players[$currentPlayer];
echo "<div>{$batsman->name}</div>\n";

Also, note that arrays in PHP are zero-based. If currentPlayer index in json data is based on 1 (rare case, but it exists sometimes) you will ned to use

另请注意,PHP中的数组是从零开始的。如果json数据中的currentPlayer索引基于1(极少数情况,但有时存在),您将需要使用

$batsman = $info->scorecards[0]->batting->players[$currentPlayer - 1];

to get right item from array.

从数组中获取正确的项目。

#3


3  

If you just want to fix your foreach

如果你只想修复你的foreach

  1. $read_json->scorecards->batting should become $read_json->scorecards[0]->batting

    $ read_json->记分卡 - >击球应成为$ read_json->记分卡[0] - >击球

  2. if($val == 5) should become if($batsmen == 5)

    if($ val == 5)if if($ batsmen == 5)

  3. $name = $batsmen->name; should become $name = $val->name;

    $ name = $ batsmen-> name;应该成为$ name = $ val-> name;

#4


2  

You need to use json_decode and array_keys for the array keys from the json.

您需要将json_decode和array_keys用于json中的数组键。

$json = '{ "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" }';
$result = json_decode ($json, true);
$keys = array_keys($result);
print_r($keys); //Array ( [0] => key1 [1] => key2 [2] => key3 )

#5


2  

In Php, You can use the code below, if you wan to fix it using foreach loop

在Php中,如果您想使用foreach循环修复它,可以使用下面的代码

foreach($json->entries as $row)

{

    foreach($row as $key => $val)
    {
        echo $key . ': ' . $val;
        echo '<br>';
    }

}

#6


1  

please have a look following code

请看下面的代码

$json=json_decode($data)->scorecards[0]->batting->players;
foreach ($json as $key => $value) {
  if($key==5){
    echo $value->name ;
  }
}

#7


1  

You can do it using converting JSON string to Array

您可以使用将JSON字符串转换为Array来实现

$json_str = '{   
"currentOver":{
    "events":[]
},  
"matchString":"",
"currentPlayer":5, 
"previousOvers":[], 
"innings":[],   
"scorecards":[
    {
        "batting":{
            "players":[
                        {"id":16447,"name":"Rahul Roy"},
                        {"id":12633,"name":"Sijal Thomas"},
                        {"id":16446,"name":"Mohammed Reza"},
                        {"id":16509,"name":"Asif Khan"},
                        {"id":12633,"name":"Koyel Dijesh"},
                        {"id":16468,"name":"Shahrook"},
                        {"id":64691,"name":"Shafiq"},
                        {"id":6518,"name":"Ubaidulah"}
            ]
        }
    }
]

}';

Decode your JSON string using "json_decode" and you get array

使用“json_decode”解码您的JSON字符串并获得数组

$json_decode_str = json_decode($json_str, true);

Now using foreach you can do anything

现在使用foreach你可以做任何事情

if($json_decode_str['scorecards']){

    foreach($json_decode_str['scorecards'] as $scorecard){

        $player_index = 1;
        if($scorecard['batting']['players']){

            foreach($scorecard['batting']['players'] as $player){

                if($player_index == 5){
                    echo $player['name'];
                }

                $player_index++;
            }

        }

    }

}

Maybe this one help you :)

也许这个帮你:)