打印不带 var_dump() 提供的元数据前缀的计算总和

发布于 2024-12-26 17:32:32 字数 695 浏览 1 评论 0原文

我有这段代码计算字符串中“字母数字”的总和,但其输出是:int(64)64T(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

悲凉≈ 2025-01-02 17:32:32

int(64)var_dump() DOC 的一项功能。只需 echo 变量,您就应该得到正常的数字输出。

int(64) is a feature of var_dump() DOCs. Just echo the variable and you should get normal numerical output.

迷迭香的记忆 2025-01-02 17:32:32

关于您的实际脚本,可以通过将每个字母的 ord() 值相加来简化(小于 96,因为 小写字母在 Ascii 表中从 97 开始)。我的脚本假设仅使用单字节小写字母作为输入。

代码:(演示)

$data = "test";
echo array_reduce(
         str_split($data),
         fn($sum, $char) => $sum + ord($char) - 96,
     );
// outputs: 64

除了该代码段之外,var_dump() 的输出还包括元数据关于价值。

您可以选择多种函数和语言结构来呈现数据。重要的是要记住,虽然技术有细微的行为,但如果您将数据打印到文档(html、txt 或其他内容),则需要将非平面数据类型转换为平面数据类型。请注意,在下面的演示中,某些数据类型在打印时显示不同或根本不显示。对于非扁平数据,我的演示会将数据编码为 JSON 字符串,但有不同的方法可以生成扁平值。

代码:(演示)

$tests = [
    'false' => false,
    'true' => true,
    'null' => null,
    '0' => 0,
    '0.1' => 0.1,
    'a' => 'a',
    '[\'foo\']' => ['foo'],
    '(object) [\'bar\', \'bar\']' => (object) ['bar', 'bar'],
];

echo "<table border=1>";
    echo '<tr><th>input</th><th>echo</th><th>printf</th><th>print_r</th><th>var_export</th><th>var_dump</th></tr>';
    foreach ($tests as $input => $test) {
        echo "<tr>";
        echo "<th>$input</th><td>";
        echo is_array($test) || is_object($test) ? json_encode($test) : $test;
        echo "</td><td>";
        printf('%s', is_array($test) || is_object($test) ? json_encode($test) : $test);
        echo "</td><td>";
        print_r($test);
        echo "</td><td>";
        var_export($test);
        echo "</td><td>";
        var_dump($test);
        echo "</td></tr>";
    }
echo "</table>";

在此处输入图像描述

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)

$data = "test";
echo array_reduce(
         str_split($data),
         fn($sum, $char) => $sum + ord($char) - 96,
     );
// outputs: 64

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)

$tests = [
    'false' => false,
    'true' => true,
    'null' => null,
    '0' => 0,
    '0.1' => 0.1,
    'a' => 'a',
    '[\'foo\']' => ['foo'],
    '(object) [\'bar\', \'bar\']' => (object) ['bar', 'bar'],
];

echo "<table border=1>";
    echo '<tr><th>input</th><th>echo</th><th>printf</th><th>print_r</th><th>var_export</th><th>var_dump</th></tr>';
    foreach ($tests as $input => $test) {
        echo "<tr>";
        echo "<th>$input</th><td>";
        echo is_array($test) || is_object($test) ? json_encode($test) : $test;
        echo "</td><td>";
        printf('%s', is_array($test) || is_object($test) ? json_encode($test) : $test);
        echo "</td><td>";
        print_r($test);
        echo "</td><td>";
        var_export($test);
        echo "</td><td>";
        var_dump($test);
        echo "</td></tr>";
    }
echo "</table>";

enter image description here

筑梦 2025-01-02 17:32:32
echo $wordCount;

而不是

var_dump($wordCount);
echo $wordCount;

instead of

var_dump($wordCount);
一口甜 2025-01-02 17:32:32

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).

怪我入戏太深 2025-01-02 17:32:32

PHP函数var_dump()另外显示变量及其类型的结构化信息到它的价值。您只需 echoprint() 变量。

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.

長街聽風 2025-01-02 17:32:32

您需要使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文