简体   繁体   English

我如何从下面的 json 对象在 php 中为我的数据库获取这个 json 值standing_place

[英]How do i fetch this json value standing_place for my database in php from the json object below

JSON: JSON:

{
  "success": 1,
  "result": {
    "total": [
      {
        "standing_place": "1",
        "standing_place_type": "Promotion - NBA (Play Offs)",
        "standing_team": "Toronto Raptors",
        "standing_P": "82",
        "standing_W": "59",
        "standing_WO": "0",
        "standing_L": "23",
        "standing_LO": "0",
        "standing_F": "9156",
        "standing_A": "8518",
        "standing_PCT": "0.720",
        "team_key": "51",
        "league_key": "787",
        "league_season": "2017/2018",
        "league_round": "Eastern Conference",
        "standing_updated": "2018-05-28 17:22:59"
      },
      {
        "standing_place": "3",
        "standing_place_type": "Promotion - NBA (Play Offs)",
        "standing_team": "Philadelphia 76ers",
        "standing_P": "82",
        "standing_W": "52",
        "standing_WO": "0",
        "standing_L": "30",
        "standing_LO": "0",
        "standing_F": "9004",
        "standing_A": "8635",
        "standing_PCT": "0.634",
        "team_key": "56",
        "league_key": "787",
        "league_season": "2017/2018",
        "league_round": "Eastern Conference",
        "standing_updated": "2018-05-28 17:23:00"
      }
  ]
}
      

PHP: PHP:

$APIkey='XXXXXXX';
$leagueId = 787;

$curl_options = array(
CURLOPT_URL => "https://allsportsapi.com/api/basketball/?met=Leagues&APIkey=$APIkey&leagueId=$leagueId",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 5
);

$curl = curl_init();
curl_setopt_array( $curl, $curl_options );
$result = curl_exec( $curl );

$result = (array) json_decode($result);

var_dump($result);

    $result =  json_decode($result);
    foreach($result as $mydata)
    {    foreach($result->result as $myresult)
        {
           foreach($myresult->total as $total)
        {
             $standing_place_type = $total->standing_place_type;
            }
        }
    }

This Json code is source from all sports APi I have work with some other Api but did not find this one fun to work with.这个 Json 代码来自所有运动 Api,我与其他一些 Api 一起工作,但没有发现使用这个很有趣。 I have try all the means know to me to solve this problem but all method prove abortive with empty result.我已经尝试了我知道的所有方法来解决这个问题,但所有方法都被证明是失败的,结果是空的。 please if you have work with the api before kindly help or if you have an idea how i can solve this problem please help请如果您在帮助之前使用过api,或者您知道我如何解决这个问题,请帮忙

What you have is basically there you just need to check that the total index exists prior to trying to use it:您所拥有的基本上就在那里,您只需要在尝试使用它之前检查total索引是否存在:

foreach($result as $mydata) {
    if(!empty($mydata->total)){
        foreach($mydata->total as $total ){
            echo $total->standing_place_type . PHP_EOL;
        }
    }
}

https://3v4l.org/TS5Ae https://3v4l.org/TS5Ae

First of all, if you're more familiar with PHP than JSON notation, try to print_r the result of json_decode to see how the structure is being converted.首先,如果您更熟悉 PHP 而非 JSON 表示法,请尝试将json_decode的结果print_r以查看结构是如何转换的。 It should be like this :应该是这样的

$decodedJson = json_decode($json);
print_r($decodedJson);

// Result
stdClass Object
(
    [success] => 1
    [result] => stdClass Object
        (
            [total] => Array
                (
                    [0] => stdClass Object
...

Can you see?你能看到吗你能明白吗? It's an object , with a result object , with a total object with an objects array .它是一个对象,一个结果对象,一个总对象和一个对象数组

Now, knowing that you can access PHP objects public properties like this $myObject->my_property and arrays you can iterate with foreach like in your sample, you would navigate this structure in PHP like this:现在,知道您可以访问像$myObject->my_property这样的 PHP 对象公共属性和可以像示例中那样使用foreach迭代的数组,您可以像这样在 PHP 中导航这个结构:


// Check if the object contains the expected structure
if ($decodedJson && isset($decodedJson->result) && isset($decodedJson->result->total)) {
    // Iterate over Total Array
   foreach ($decodedJson->result->total as $total) {
       // Print values
       echo "Standing_Place: {$total->standing_place} / Standing_Place_Type: {$total->standing_place_type}." . PHP_EOL;
   } 
}

Live sample . 现场样品

Obs: On the JSON you posted, there is a curly bracket missing in the end. Obs:在您发布的 JSON 中,最后缺少一个大括号。 You can validate the json this site .您可以验证此站点的 json。

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

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