打印不带 var_dump() 提供的元数据前缀的计算总和
我有这段代码计算字符串中“字母数字”的总和,但其输出是:int(64)
。 64
是 T
(20)、E
(2)、S
(19)、的总和>T
(20) --> 20+5+19+20=64
。
我只希望数字作为输出 64
而不是 int(64)
。
这是代码:
$data = "test";
$testResult = array_values(
array_merge(
array_fill_keys(
range('A','Z'),
0
),
array_count_values(
str_split(
strtoupper($data)
)
)
)
);
$wordCount = 0;
foreach ($testResult as $letterValue => $letterCount) {
$wordCount += ++$letterValue * $letterCount;
}
var_dump($wordCount);
I have this code which calculates the sum of "letter numbers" in a string, but its output is: int(64)
. 64
is the total of T
(20), E
(2), S
(19), T
(20) --> 20+5+19+20=64
.
I want only number as output 64
instead of int(64)
.
Here is the code:
$data = "test";
$testResult = array_values(
array_merge(
array_fill_keys(
range('A','Z'),
0
),
array_count_values(
str_split(
strtoupper($data)
)
)
)
);
$wordCount = 0;
foreach ($testResult as $letterValue => $letterCount) {
$wordCount += ++$letterValue * $letterCount;
}
var_dump($wordCount);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
int(64)
是var_dump()
DOC 的一项功能。只需echo
变量,您就应该得到正常的数字输出。int(64)
is a feature ofvar_dump()
DOCs. Justecho
the variable and you should get normal numerical output.关于您的实际脚本,可以通过将每个字母的
ord()
值相加来简化(小于 96,因为 小写字母在 Ascii 表中从 97 开始)。我的脚本假设仅使用单字节小写字母作为输入。代码:(演示)
除了该代码段之外,
var_dump()
的输出还包括元数据关于价值。您可以选择多种函数和语言结构来呈现数据。重要的是要记住,虽然技术有细微的行为,但如果您将数据打印到文档(html、txt 或其他内容),则需要将非平面数据类型转换为平面数据类型。请注意,在下面的演示中,某些数据类型在打印时显示不同或根本不显示。对于非扁平数据,我的演示会将数据编码为 JSON 字符串,但有不同的方法可以生成扁平值。
代码:(演示)
Regarding your actual script, it can be simplified by summing the the
ord()
value of each letter (less 96, because lowercase letters start from 97 on the Ascii table). My script assumes that only single-byte lowercase letters will be used as input.Code: (Demo)
Beyond that snippet,
var_dump()
's output includes metadata about the value.There are several functions and language constructs that you can choose from to present your data. It is important to remember though that while technique has nuanced behaviour, if you are printing data to a document (html, txt, or something else), non-flat data types will need to be converted to a flat data type. Notice in the demonstration below that some data types are displayed differently or not at all when printed. For non-flat data, my demo will encode the data as a JSON string, but there are different ways to generate a flatten value.
Code: (Demo)
而不是
instead of
var_dump 为您提供有关变量的所有信息, (int) 让您知道它是一个整数,如果您只是回显它,您将看不到 (int)。
var_dump gives you all information about a variable the (int) lets you know it is an integer, if you just echo it out you won't see the (int).
PHP函数var_dump()另外显示变量及其类型的结构化信息到它的价值。您只需 echo 或 print() 变量。
PHP function var_dump() displays structured information the variable and its type in addition to its value. You simply need to echo or print() the variable.
您需要使用 echo 而不是 var_dump。 Var_dump 为您提供有关变量的附加信息。如果 $wordCount 是一个数组而不是一个 int,它将显示 Array(size){values...}
阅读 vardump 的手册 这里
我希望这能解决您的问题。
问候,
斯特凡
You need to use echo instead of var_dump. Var_dump gives you additional information about the variable. If $wordCount was an array instead of an int, it will display Array(size){values...}
read the manual of vardump here
i hope this will solve your problem.
greets,
stefan