PHP:计算包含给定参数和给定值的多级数组的所有项

时间:2017-07-15 12:42:46

标签: 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 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 [keys]来检查这种方式。

我想,如果我应该为params[key]分配一个默认值(如通配符),那么,如果没有给出,该函数将获取所有$ item。我的意思是$item[0]总是(==)等于的东西。谢谢。 [见我的解决方案答案]

2 个答案:

答案 0 :(得分:2)

声明函数的方式, 传递参数。你想要做的是有一个默认值,以便你的函数内部的代码可以检测到:

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

如果这不是您要查找的结果,则需要重写您的问题。

编辑#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

在这种情况下,为数组指定了显式字符串,而字符串&#39;值将与大多数情况下的数字索引相同,字符串索引与数字索引相同。

您在答案中提出的代码有一些错误:

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]进行这些测试,因为$key0

'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

由于您的原始问题并不十分明确,我想到您可能正在尝试做的事情是计算{{1}中事物的类型的数量而不是获得每个请求类别中的项目总数。在这种情况下,$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 5 myfunction(array('banana')); // returns 1 myfunction(array('apple','eggs')); // returns 2 myfunction(array('tomatoes')); // returns 0 - not in $myArray 的样本

答案 1 :(得分:-1)

我的解决方案

(未经过广泛测试,但到目前为止有效)

关注@FKEinternet回答,这是适合我的解决方案:显然$ params应该使用$ item的相同键。因此,如果在调用函数时给出参数[&#39; mykey&#39;]并且其值等于item [&#39; mykey&#39;],则对foreach迭代进行迭代,计算迭代次数和$ 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;
}

感谢大家的所有投入!!