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

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%)
duanchun6148
duanchun6148 我编辑了我的帖子,ty为你的帮助!
一年多之前 回复
dsaf32131
dsaf32131 为什么然后你不向我们展示真正的阵列,我们还需要声明$res[“Total”]!没有它们很难测试。..并且需要时间去做自己的事情。..
一年多之前 回复
dowy77780
dowy77780 除了我以前的评论:str_pad自动计算差异,它只需要$max_row_length作为注释,或者你可以手动计算差异并在str_repeat函数的帮助下生成空格字符串
一年多之前 回复
doupinge9055
doupinge9055 $breakdown是数组的名称,$res[“Total”]是在此脚本之前计算的数组内所有数量的总和,但我知道它运行良好
一年多之前 回复
douhuan1979
douhuan1979 你的阵列叫什么名字?$击穿?并设置$res[“Total”]?
一年多之前 回复
douchi0638
douchi0638 所以对于每一列我必须计算我必须添加多少空格?有没有更有效的方法来做到这一点?
一年多之前 回复
donpvtzuux37724
donpvtzuux37724 什么都没有:(@DanielE。
一年多之前 回复
dongtu1958
dongtu1958 str_pad和空格而不是制表符号是你的朋友:你应该找到每列的字符串的最大长度,然后为每一行添加额外的空格($spaces_to_add=$max_row_length=$row_length)
一年多之前 回复
douzhan4522
douzhan4522 实际结果是什么?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问