Home  >  Article  >  Backend Development  >  asp - 一个PHP算法方面的计算题。

asp - 一个PHP算法方面的计算题。

WBOY
WBOYOriginal
2016-06-06 20:15:181079browse

<code><?php aaa    =    trim(request("aaa"))
bbb    =    trim(request("bbb"))
ccc    =    trim(request("ccc"))
ddd    =    trim(request("ddd"))
eee    =    trim(request("eee"))
fff    =    trim(request("fff"))
ggg    =    trim(request("ggg"))
hhh    =    trim(request("hhh"))
。。。。。。。。。。
。。。。。。。。。。
?>
</code>

如何用PHP快速计算出本页面到底接收了几个不为空的参数?,并把参数个数及参数值逐一echo出来。

(注意:这些参数中,如果是带有半角逗号或空格时,需要按照多参数来计算。并用explode拆分开,也逐一echo出来测试一下)

回复内容:

<code><?php aaa    =    trim(request("aaa"))
bbb    =    trim(request("bbb"))
ccc    =    trim(request("ccc"))
ddd    =    trim(request("ddd"))
eee    =    trim(request("eee"))
fff    =    trim(request("fff"))
ggg    =    trim(request("ggg"))
hhh    =    trim(request("hhh"))
。。。。。。。。。。
。。。。。。。。。。
?>
</code>

如何用PHP快速计算出本页面到底接收了几个不为空的参数?,并把参数个数及参数值逐一echo出来。

(注意:这些参数中,如果是带有半角逗号或空格时,需要按照多参数来计算。并用explode拆分开,也逐一echo出来测试一下)

<code><?php foreach ($_REQUEST as $key => $value) {
    if (trim($value)) {
        $values = preg_split("/[\s,]+/", $value);
        foreach ($values as $v) {
            echo $v . '\n';
        }
    }
}</code>

这个和算法有关系吗?

<code><?php $_REQUEST = ['a' => 'a,b,c', 'b' => '', 'c' => 'a,d c0 ,0'];
$arr = [];
foreach($_REQUEST as $v) {
    $v = str_replace(" ", ',', $v);
    $v = trim($v, ' ,');
    $v = explode(',', $v);
    foreach($v as $_v) {
        $_v = trim($_v);
        if (strlen($_v)) $arr[] = $_v;
    }
}
print_r($arr);
echo count($arr);
/*
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => a
    [4] => d
    [5] => c
    [6] => c0
    [7] => 0
)
8
*/</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn