使用php获取json密钥的索引

时间:2016-06-07 09:37:52

标签: php json

我想找到json数组中的键索引,类似于这个问题Get index of a key in json 但我需要使用php的解决方案。

这是我的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"}
            ]
        }
    }
]

}

和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"; 

                }               
}

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

7 个答案:

答案 0 :(得分:7)

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

http://codepad.org/XQDCKAsB

也可以在下面找到代码示例。

       $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; 

答案 1 :(得分:3)

试试这段代码。

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

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

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

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

答案 2 :(得分:3)

如果您只想修复foreach

  1. $read_json->scorecards->batting应该成为$read_json->scorecards[0]->batting

  2. if($val == 5)应该成为if($batsmen == 5)

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

答案 3 :(得分:2)

您需要使用json_decodearray_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 )

答案 4 :(得分:2)

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

foreach($json->entries as $row)

{

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

}

答案 5 :(得分:1)

请查看以下代码

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

答案 6 :(得分:1)

您可以使用将JSON字符串转换为数组

来实现
$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"}
            ]
        }
    }
]

}';

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

$json_decode_str = json_decode($json_str, true);

现在使用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++;
            }

        }

    }

}

也许这个可以帮助你:)