PHP:计算包含给定参数和给定值的多级数组的所有项
我试图构建一个函数来计算包含给定参数的数组的所有项,但是,如果调用该函数时没有给出该参数,则该函数应该计算所有项。参数是通过数组PHP:计算包含给定参数和给定值的多级数组的所有项,php,if-statement,Php,If Statement,我试图构建一个函数来计算包含给定参数的数组的所有项,但是,如果调用该函数时没有给出该参数,则该函数应该计算所有项。参数是通过数组$params传递的:这是我到目前为止所做的: function myfunction($params){ global $myArray; if ( !isset($params[0]) ){ $params[0] = ???????? } // I need a wildcard here, so that, if the parameter is not
$params
传递的:这是我到目前为止所做的:
function myfunction($params){
global $myArray;
if ( !isset($params[0]) ){ $params[0] = ???????? } // I need a wildcard here, so that, if the parameter is not given, the condition will be true by default
if ( !isset($params[1]) ){ $params[1] = ???????? } // I need a wildcard here, so that, if the parameter is not given, the condition will be true by default
....etc......
foreach($myArray as $item){
if ($item[0] == $params[0]){ // this condition should be true if parameter is not given
if ($item[1] == $params[1]){// this condition should be true if parameter is not given
$count += $item
}
}
}
return $count;
}
我想:
myfunction(); //counts everything
myfunction( array ('0' => 'banana') ); //counts only $myArray['0'] = banana
myfunction( array ('0' => 'apple', '1' => 'eggs') ); //counts only $myArray['0'] = apples and $myArray['1'] = eggs
我有几个$params[键]要这样检查
我想,如果我应该为
params[key]
(像一个通配符)指定一个默认值,那么如果没有指定,函数将接受所有$item。我的意思是,$item[0]
始终等于(=)的东西。谢谢[请参阅我的答案以获取解决方案]函数的声明方式必须传递一个参数。您要做的是设置一个默认值,以便函数中的代码能够检测到:
function myfunction($params=NULL)
{
global $myArray;
if (empty($params))
{
// count everything
}
else
{
// count what's listed in the $params array
}
}
编辑
如果我正确阅读了您的评论,$myArray
看起来像这样:
$myArray=array
(
'apple'=>3, // 3 apples
'orange'=>4, // 4 oranges
'banana'=>2, // 2 bananas
'eggs'=>12, // 12 eggs
'coconut'=>1, // 1 coconut
);
假设这是真的,你想要的是
function myfunction($params=NULL)
{
global $myArray;
$count=0;
if (empty($params)) // count everything
{
foreach ($myArray as $num) // keys are ignored
$count += $num;
}
else if (!is_array($params)) // sanity check
{
// display an error, write to error_log(), etc. as appropriate
}
else // count what's listed in the $params array
{
foreach ($params as $key) // check each item listed in $params
if (isset($myArray[$key])) // insure request in $myArray
$count += $myArray[$key]; // add item's count to total
}
return $count;
}
这会给你
myfunction(); // returns 22
myfunction(array('banana')); // returns 2
myfunction(array('apple','eggs')); // returns 15
myfunction(array('tomatoes')); // returns 0 - not in $myArray
myfunction(); // returns 5
myfunction(array('banana')); // returns 1
myfunction(array('apple','eggs')); // returns 2
myfunction(array('tomatoes')); // returns 0 - not in $myArray
如果这不是你想要的结果,你需要重写你的问题
编辑#2
请注意,由于未指定显式键的数组按元素列出的顺序进行数字键控,因此我上面显示的函数调用与以下函数完全等效:
myfunction(); // returns 22
myfunction(array(0=>'banana')); // returns 2
myfunction(array(0=>'apple',1=>'eggs')); // returns 15
myfunction(array(0=>'tomatoes')); // returns 0 - not in $myArray
myfunction(); // returns 22
myfunction(array('0'=>'banana')); // returns 2
myfunction(array('0'=>'apple','1'=>'eggs')); // returns 15
myfunction(array('0'=>'tomatoes')); // returns 0
但是,这些调用不等同于以下调用:
myfunction(); // returns 22
myfunction(array(0=>'banana')); // returns 2
myfunction(array(0=>'apple',1=>'eggs')); // returns 15
myfunction(array(0=>'tomatoes')); // returns 0 - not in $myArray
myfunction(); // returns 22
myfunction(array('0'=>'banana')); // returns 2
myfunction(array('0'=>'apple','1'=>'eggs')); // returns 15
myfunction(array('0'=>'tomatoes')); // returns 0
在这种情况下,将为数组指定显式字符串键,虽然字符串的值在大多数情况下的计算结果与数值索引相同,但字符串索引与数值索引不同
您在回答中提出的代码有几个错误:
foreach($myArray as $item)
{
foreach ($params as $key => $value)
{
if ( isset($params[$key]) && $params[$key] == $item[$key] )
{
$count += $item
}
}
}
首先,isset($params[$key])
将根据性质或数组和foreach
始终计算为TRUE。其次,由于您的外部foreach
循环,如果您的$myArray
是如上所示的结构,那么调用myfunction(array('apple'))
将导致$params[$key]==$item[$key]
进行这些测试,因为$key
是0
:
'apple' == 'apple'[0] // testing 'apple' == 'a'
'apple' == 'orange'[0] // testing 'apple' == 'o'
'apple' == 'banana'[0] // testing 'apple' == 'b'
'apple' == 'eggs'[0] // testing 'apple' == 'e'
'apple' == 'coconut'[0] // testing 'apple' == 'c'
如您所见,这不会产生预期的结果
代码的第三个问题是,$count+=$item
行的末尾没有分号,因此我猜您在提出答案之前没有尝试运行此代码
编辑#3
由于您最初的问题并不十分清楚,我突然想到,您可能想做的是计算$myArray
中的物品类型数量,而不是计算每个请求类别中的物品总数。在这种情况下,myfunction()
的最后一个分支更简单:
else // count what's listed in the $params array
{
foreach ($params as $key) // check each item listed in $params
if (isset($myArray[$key])) // insure request in $myArray
$count++; // add the item to the total
}
对于我演示的示例$myArray
,上面的更改将为您提供
myfunction(); // returns 22
myfunction(array('banana')); // returns 2
myfunction(array('apple','eggs')); // returns 15
myfunction(array('tomatoes')); // returns 0 - not in $myArray
myfunction(); // returns 5
myfunction(array('banana')); // returns 1
myfunction(array('apple','eggs')); // returns 2
myfunction(array('tomatoes')); // returns 0 - not in $myArray
同样,如果这两个结果都不是您想要的,您需要重写您的问题,并包括
$myArray
我的解决方案的示例
(未进行广泛测试,但迄今为止仍有效)
在@FKEinternet answer之后,这是一个适合我的解决方案:$params显然应该使用与$item相同的键。因此,对于每次迭代,如果调用函数时给出了参数['mykey'],且其值等于项['mykey'],则计算迭代次数,$count增长+1
function myfunction($params=NULL){
global $myArray;
foreach($myArray as $item){
foreach ($params as $key => $value){
if ( isset($params[$key]) && $params[$key] == $item[$key] ){
$count += $item;
}
}
}
return $count;
}
谢谢大家的投入 阅读
if
附带的else
如果要设置默认值,为什么不创建一个默认值数组,并对传入的$params
数组使用array\u merge()
呢?这个问题不可理解@谢谢,但我不知道怎么做。您能告诉我如何实现else
im my函数,以便在未给出$params[0]
的情况下让它计算所有项目吗?@Terryarray\u merge()
可能不会给出所需的结果:,“如果要将第二个数组中的数组元素追加到第一个数组中,同时不覆盖第一个数组中的元素,也不重新编制索引,请使用+
数组联合运算符”-函数输入参数为第一个(左)数组,默认值为第二个(右)在这种情况下是数组。谢谢FKE。我想出了一个适合我的解决方案。我喜欢你的,但我不知道当你嵌套if-else时如何在else语句中计数,就像在我的例子中一样。你能在你的解决方案中显示它吗?你的$myArray看起来像什么?嵌套if-else真的需要吗?顺便说一句,要求更新答案,然后不看就离开可能被认为是不礼貌的-如果你不想看到我的答案是什么,请我花时间充实我的答案有什么意义?对不起,我不是故意不礼貌或滥用你的时间…但有时我需要去工作来付我的账单…;)我相信你会理解的。我现在回来了,我会回来的好的,我会及时通知您,并确定您的答案。再次感谢!请务必让我知道我对您的问题的解释是否正确,以及我的代码是否有帮助。在将其作为答案提出之前,您是否尝试运行此代码?(请参阅我的编辑#2了解我认为您没有这样做的原因。)在打折之前,您应该尝试实际运行我的代码-除非您的$myArray
的结构与我演示的不同,否则我的代码将为您提供所需的结果。如果您的$myArray
的结构与我的不同,请编辑您的问题,以说明正确的$myArray
结构。