简体   繁体   English

如何格式化sql值“ [a,b,c]”?

[英]How do I format the sql value “[a,b,c]”?

On a game, there are three different teams. 在一个游戏中,有三个不同的团队。 For example, team a, b, and c. 例如,团队a,b和c。 On the game's database the playtime is reported like this: "[a,b,c]" and each letter is how many minutes the user has played on that specific team. 在游戏的数据库上,游戏时间的报告如下:“ [a,b,c]”,每个字母代表用户在该特定团队中的游戏时间。 I used PHP to grab the data from the database to display on a website, but it is just displaying as "[14,0,1]" as I have 14 minutes on team a, 0 on team b, and 1 on team c. 我使用PHP从数据库中获取数据以显示在网站上,但是由于我在团队a上有14分钟,在团队b上有0分钟,在团队c上有1分钟,因此它只是显示为“ [14,0,1]” 。

How can I format this, through PHP or JSON, to add a, b, and c, or in this case 14+0+1? 如何通过PHP或JSON设置其格式,以添加a,b和c,或者在这种情况下添加14 + 0 + 1?

The code in PHP: PHP中的代码:

$sql = "SELECT * FROM players WHERE playerid ='". $steamprofile['steamid']  
."'";
$result = $db->query($sql);
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $playtime = $row["playtime"];
    }

The code in HTML: HTML中的代码:

<span id="label">Playtime</span>: <b><span id="playerPlaytime"><?php echo 
$playtime; ?></span></b><br>

Output: http://prntscr.com/jum85v 输出: http : //prntscr.com/jum85v

Create an array by splitting on commas and then calculate the sum (trim extra stuff first): 通过分割逗号创建一个数组,然后计算总和(首先修剪多余的东西):

$playtime = array_sum(explode(',', trim($row['playtime'], '"[]')));

To use JSON you still need to trim the quotes: 要使用JSON,您仍然需要修剪引号:

$playtime = array_sum(json_decode(trim($row['playtime'], '"')));

如果数据库中的变量是一个字符串(它似乎是一个JSON对象),那么在PHP中,您可以这样做:

$playtime = array_sum(json_decode($row['playtime']));

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

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