从 php 中的数组中的对称整数中获取总数?
Get a total from symmetric intigers from array in php?
好的,所以我有数组,例如 $arr= "/43sdsd555ksldk66sd"544fdfd";
我使用 preg_match_all '/\d+/'
和 array_map('intval', $zni[0]);
取数
现在的问题是我需要反转这些整数以查看它们是否对称,例如 555 和 66,以及它们是否是对称的。(只有对称数的总和)
我尝试使用函数“strrev”并得到对称数,但我不知道如何将它们放在一个地方(如果它们是对称的)并计算它们。
<?php
$numbers = "";
if (isset($_GET['submit']))
{
$numbers = ($_GET['niz']);
preg_match_all('/\d+/', $numbers, $zni);
$numtwo= array_map('intval', $zni[0]);
}
foreach ($numtwo as $num)
{
$reverse = strrev($num);
var_dump($reverse);
if ($num == $reverse)
{
$reverse = "true";
} else {
$reverse = "false";
}
var_dump($reverse);
}
既然你已经完成了大部分的工作,而你所缺少的基本上就是使用 +
或 +=
,这里有一个简单的例子来说明如何做到这一点:
$input = "/43sdsd555ksldk66sd544fdfd";
$total = 0;
preg_match_all('/\d+/', $input, $m);
foreach ($m[0] as $d)
if ($d == strrev($d))
$total += $d;
var_dump($total); // => int(621)
没有必要使用 intval()
,因为 PHP 将根据需要在类型之间隐式转换。
或者,您可以用 PHP 的 array_*
函数替换循环:
$input = "/43sdsd555ksldk66sd544fdfd";
preg_match_all('/\d+/', $input, $m);
$total = array_sum(array_filter($m[0], function ($v) { return $v == strrev($v); }));
var_dump($total); // => int(621)
这里我们使用匿名函数array_filter()
to generate a new array that only contains palindrome numbers from the original matches which is then given to array_sum()
。
因此,将您的原始代码转换为工作示例所需要做的就是引入一个变量并总结:
<?php
$numbers = "";
if (isset($_GET['submit']))
{
$numbers = ($_GET['niz']);
preg_match_all('/\d+/', $numbers, $zni);
$numtwo= array_map('intval', $zni[0]);
}
$total = 0; // new variable
foreach ($numtwo as $num)
{
$reverse = strrev($num);
var_dump($reverse);
if ($num == $reverse)
{
$reverse = "true";
$total += $num; // sum up
} else {
$reverse = "false";
}
var_dump($reverse);
}
var_dump($total); // => int(621)
好的,所以我有数组,例如 $arr= "/43sdsd555ksldk66sd"544fdfd";
我使用 preg_match_all '/\d+/'
和 array_map('intval', $zni[0]);
现在的问题是我需要反转这些整数以查看它们是否对称,例如 555 和 66,以及它们是否是对称的。(只有对称数的总和)
我尝试使用函数“strrev”并得到对称数,但我不知道如何将它们放在一个地方(如果它们是对称的)并计算它们。
<?php
$numbers = "";
if (isset($_GET['submit']))
{
$numbers = ($_GET['niz']);
preg_match_all('/\d+/', $numbers, $zni);
$numtwo= array_map('intval', $zni[0]);
}
foreach ($numtwo as $num)
{
$reverse = strrev($num);
var_dump($reverse);
if ($num == $reverse)
{
$reverse = "true";
} else {
$reverse = "false";
}
var_dump($reverse);
}
既然你已经完成了大部分的工作,而你所缺少的基本上就是使用 +
或 +=
,这里有一个简单的例子来说明如何做到这一点:
$input = "/43sdsd555ksldk66sd544fdfd";
$total = 0;
preg_match_all('/\d+/', $input, $m);
foreach ($m[0] as $d)
if ($d == strrev($d))
$total += $d;
var_dump($total); // => int(621)
没有必要使用 intval()
,因为 PHP 将根据需要在类型之间隐式转换。
或者,您可以用 PHP 的 array_*
函数替换循环:
$input = "/43sdsd555ksldk66sd544fdfd";
preg_match_all('/\d+/', $input, $m);
$total = array_sum(array_filter($m[0], function ($v) { return $v == strrev($v); }));
var_dump($total); // => int(621)
这里我们使用匿名函数array_filter()
to generate a new array that only contains palindrome numbers from the original matches which is then given to array_sum()
。
因此,将您的原始代码转换为工作示例所需要做的就是引入一个变量并总结:
<?php
$numbers = "";
if (isset($_GET['submit']))
{
$numbers = ($_GET['niz']);
preg_match_all('/\d+/', $numbers, $zni);
$numtwo= array_map('intval', $zni[0]);
}
$total = 0; // new variable
foreach ($numtwo as $num)
{
$reverse = strrev($num);
var_dump($reverse);
if ($num == $reverse)
{
$reverse = "true";
$total += $num; // sum up
} else {
$reverse = "false";
}
var_dump($reverse);
}
var_dump($total); // => int(621)