dongliqin6939 2018-12-21 14:36
浏览 97

我可以存储该函数的字符串结果并将其连接到另一个字符串吗?

I'm reading an array from db and i want to format that to provide a table like text for my telegram bot.

I try to use the sprintf php funtion to format the text, but when i try to put the result into a string the result is that any string was putted in the string.

This is my array:

// This is my complete array.
{
"result":true,
"error":"nothing",
"content":{
    "3269":{"Quantity":4,"Name":"Atom Seikhart"},
    "3273":{"Quantity":4,"Name":"Brunhild"},
    "3304":{"Quantity":3,"Name":"Isis"},
    "3319":{"Quantity":1,"Name":"Arthur"},
    "3329":{"Quantity":2,"Name":"Loki"},
    "3343":{"Quantity":1,"Name":"Chamimi"},
    "3351":{"Quantity":14,"Name":"Hanzo"},
    "3379":{"Quantity":8,"Name":"Lucifer"}
    },
"Total":37
}

And this is my php cycle and message send:

// This is my "select from db" function.
$res = get_event_rulers_breakdowns_by_id($mysqli, $value["Id"]);
$breakdown = $res["content"];
$message .= "
This is the event ruler breakdown (total " . $res["Total"] . "):
";
foreach ($breakdown as $value) {
    $perc = round($value["Quantity"] / $res["Total"] * 100, 2);
    $stringa = "%+4s:%+4d\t(<code>%+4g%</code>)
";

    // This work but don't format the text.
    $message .= $value["Name"] . ":\t" . $value["Quantity"] . " (<code>" . $perc . "%</code>)
";

    // This dosen't work.
    $formatt = sprintf("%+4s:%+4d (<code>%+4g%</code>)", $value["Name"], $value["Quantity"], $perc);

    // This dosen't work too.
    $formatt = sprintf("%+4s:%+4d (<code>%+4g%</code>)", "aa", 123, 12.3);
    $message .= $formatt . "
";
}

The actual result is:

And the expected result will be like:

Atom Seikhart: 4 (10.81%)
Brunhild:      4 (10.81%)
Isis:          3  (8.11%)
Arthur:        1   (2.7%)
Loki:          2  (5.41%)
Chamimi:       1   (2.7%)
Hanzo:        14 (37.84%)
Lucifer:       8 (21.62%)

or like:

Atom Seikhart:  4 (10.81%)
     Brunhild:  4 (10.81%)
         Isis:  3  (8.11%)
       Arthur:  1   (2.7%)
         Loki:  2  (5.41%)
      Chamimi:  1   (2.7%)
        Hanzo: 14 (37.84%)
      Lucifer:  8 (21.62%)
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制