如何将多维数组中的所有数字相加?
How do I add all the numbers in a multidimensional array?
我有一个包含一些 Ip 和一些数字的多维数组。我想为每个 IP 添加数组中的所有数字
所以如果我 return 我的数组到浏览器它看起来像这样:
Array (
[72.00.00.000] => 9962 9980 9984 215 9997
[90.00.00.000] => 6157 1586 8422 336
现在我想为每个 IP 添加数组中的数字
那么它应该是这样的:
[72.00.00.000] => 40138
[90.00.00.000] => 16501
这就是我的代码现在的样子:
foreach ($topTenIp as $val) {
$bytes_ips[$val] = shell_exec("grep $val /var/www/laravel/logs/vhosts/domain.log | awk '{print }'");
}
foreach ( $bytes_ips as $ip => $numList ) {
$tot = array_sum(explode(' ', $numList));
echo sprintf("%s => %d\n", $ip, $tot);
}
我得到的结果是:
72.00.00.000 => 9962
90.00.00.000 => 6157
已解决代码:
foreach ($topTenIp as $val) {
$bytes_ips[$val] = shell_exec("grep $val /var/www/laravel/logs/vhosts/domain.log | awk '{print }'");
}
foreach ( $bytes_ips as $ip => $numList ) {
$tot = array_sum(explode("\n", $numList));
echo sprintf("[%s] => %d\n", $ip, $tot);
}
然后你将不得不分别处理每个出现的 $byte_ips
并将所有 space 分隔的数字分解成一个数组,然后将数组相加。像这样
$byte_ips = array(
'72.00.00.000' => '9962 9980 9984 215 9997',
'90.00.00.000' => '6157 1586 8422 336'
);
// debugging only
print_f($byte_ips):
// end debugging
foreach ( $byte_ips as $ip => $numList ) {
$tot = array_sum(explode(' ', $numList));
echo sprintf("[%s] => %d\n", $ip, $tot);
}
结果是:
[72.00.00.000] => 40138
[90.00.00.000] => 16501
现在你发现 $byte_ips
数组中的数字实际上是由换行符分隔的,而不是像你原来的问题所说的 spaces,代码应该是:-
foreach ( $byte_ips as $ip => $numList ) {
$tot = array_sum(explode("\n", $numList));
echo sprintf("[%s] => %d\n", $ip, $tot);
}
如果您想要以兆字节为单位的结果,那么您必须将实际上是数字的字符串表示形式的数字转换为整数,这样您才能真正对其进行算术运算,然后更改 sprintf
将结果输出为浮点数
foreach ( $byte_ips as $ip => $numList ) {
$tot = array_sum(explode("\n", $numList));
$tot = ((int)$tot / 1024 / 1024);
echo sprintf("[%s] => %.6f\n", $ip,$tot);
}
现在你得到
[72.00.00.000] => 0.038279
[90.00.00.000] => 0.015737
我有一个包含一些 Ip 和一些数字的多维数组。我想为每个 IP 添加数组中的所有数字
所以如果我 return 我的数组到浏览器它看起来像这样:
Array (
[72.00.00.000] => 9962 9980 9984 215 9997
[90.00.00.000] => 6157 1586 8422 336
现在我想为每个 IP 添加数组中的数字
那么它应该是这样的:
[72.00.00.000] => 40138
[90.00.00.000] => 16501
这就是我的代码现在的样子:
foreach ($topTenIp as $val) {
$bytes_ips[$val] = shell_exec("grep $val /var/www/laravel/logs/vhosts/domain.log | awk '{print }'");
}
foreach ( $bytes_ips as $ip => $numList ) {
$tot = array_sum(explode(' ', $numList));
echo sprintf("%s => %d\n", $ip, $tot);
}
我得到的结果是:
72.00.00.000 => 9962
90.00.00.000 => 6157
已解决代码:
foreach ($topTenIp as $val) {
$bytes_ips[$val] = shell_exec("grep $val /var/www/laravel/logs/vhosts/domain.log | awk '{print }'");
}
foreach ( $bytes_ips as $ip => $numList ) {
$tot = array_sum(explode("\n", $numList));
echo sprintf("[%s] => %d\n", $ip, $tot);
}
然后你将不得不分别处理每个出现的 $byte_ips
并将所有 space 分隔的数字分解成一个数组,然后将数组相加。像这样
$byte_ips = array(
'72.00.00.000' => '9962 9980 9984 215 9997',
'90.00.00.000' => '6157 1586 8422 336'
);
// debugging only
print_f($byte_ips):
// end debugging
foreach ( $byte_ips as $ip => $numList ) {
$tot = array_sum(explode(' ', $numList));
echo sprintf("[%s] => %d\n", $ip, $tot);
}
结果是:
[72.00.00.000] => 40138
[90.00.00.000] => 16501
现在你发现 $byte_ips
数组中的数字实际上是由换行符分隔的,而不是像你原来的问题所说的 spaces,代码应该是:-
foreach ( $byte_ips as $ip => $numList ) {
$tot = array_sum(explode("\n", $numList));
echo sprintf("[%s] => %d\n", $ip, $tot);
}
如果您想要以兆字节为单位的结果,那么您必须将实际上是数字的字符串表示形式的数字转换为整数,这样您才能真正对其进行算术运算,然后更改 sprintf
将结果输出为浮点数
foreach ( $byte_ips as $ip => $numList ) {
$tot = array_sum(explode("\n", $numList));
$tot = ((int)$tot / 1024 / 1024);
echo sprintf("[%s] => %.6f\n", $ip,$tot);
}
现在你得到
[72.00.00.000] => 0.038279
[90.00.00.000] => 0.015737