Get json key index using php

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

here is my json (partial data)

{   
"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 solve this problem. Thanks in advance.

+4
source share
7 answers

I think that would be enough for your requirement.

http://codepad.org/XQDCKAsB

Find also sample code.

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

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 the index currentPlayerin json data is based on 1 (a rare case, but it exists sometimes), you can use

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

.

+3

foreach

  • $read_json->scorecards->batting $read_json->scorecards[0]->batting

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

  • $name = $batsmen->name; $name = $val->name;

+3

json json_decode array_keys.

$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 )
+2
source

In Php , you can use the code below if you want to fix it with a loopforeach

foreach($json->entries as $row)

{

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

}
+2
source

Please see the following code

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

You can do this using JSON string conversion to 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 a JSON string using "json_decode" and you will get an array

$json_decode_str = json_decode($json_str, true);

Now, using foreach, you can do something

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 will help you :)

+1
source

All Articles