开发者

Looping through arrays mySQL insert

开发者 https://www.devze.com 2023-02-12 11:14 出处:网络
I need to loop through these arrays and put the values in their respective table which the table name would be medal+key i.e medal117

I need to loop through these arrays and put the values in their respective table which the table name would be medal+key i.e medal117

Here is the script:

<?php

//incude sql database connection
include_once('sql.php');
//include api key 
include_once('api.php');

$gameId = "448434948";
$gamertag = "Jam1efoster";

$GetGameDetails = "http://www.bungie.net/api/reach/reachapijson.svc/game/details/".$apiKey."/".$gameId;

$output = file_get_contents($GetGameDet开发者_如何学Cails);
$obj = json_decode($output);
//echo $output."<br/>";


foreach($obj->GameDetails->Players as $players) {
    if ($players->PlayerDetail->gamertag == $gamertag) {
        foreach($players->SpecificMedalCounts as $SpecificMedalCounts) {
            $medal = $SpecificMedalCounts;
            echo '<pre>'.print_r($medal,1).'</pre>';
        }
    }
}

?>

Currently it outputs:

stdClass Object
(
    [Key] => 117
    [Value] => 1
)
stdClass Object
(
    [Key] => 2
    [Value] => 1
)
stdClass Object
(
    [Key] => 85
    [Value] => 8
)
stdClass Object
(
    [Key] => 101
    [Value] => 1
)


Are you after anything more than this? Assuming you have already made the connection the the database.

mysql_query(sprintf(
    'INSERT INTO `medal%d` SET `value` = %d', $medal->Key, $medal->Value
));

Based on your comment to your questions, this is more about accessing the properties on a class.

From the docs, linked below:

Within class methods the properties, constants, and methods may be accessed by using the form $this->property

http://php.net/manual/en/language.oop5.properties.php

0

精彩评论

暂无评论...
验证码 换一张
取 消