dousui6193 2016-09-20 17:02
浏览 63
已采纳

根据存在的数组键的数量动态构建if语句

I have a PHP function in which I pass in a string or array of column names. Unfortunately, this can become a bit problematic and cumbersome depending on the number of column names that I get.

As such, is there a way that I can rewrite my if statement so that I dynamically can build the conditional based on the number of elements I have in my array?

I would usually run a check to see if a value was in the array, however as these are key values of another array, it makes things more complicated. Below is my current code.

private function pushToArray($list, $columnName, $date) {
    $array = [];
    foreach($list as $item) {
        if (is_array($columnName)){
            if ($date == $item[$columnName[0]] || $date == $item[$columnName[1]]) {
                array_push($array, $item);
            }
        } else {
            if ($date == $item[$columnName]) {
                array_push($array, $item);
            }
        }
    }
    return $array;
}

As can be seen here, as my array increase in elements, I would have to add additional if conditions to handle the larger array set. Something I would like to avoid.

EDIT: Right now, I am using laravel as the framework for my application. I am trying to generate an array that will populate a view of an upcoming event system. Here is a sample of the current calls:

$separatedArray = $this->pushToArray($this->separatedList(), 'destroyed_date', $currentDate);
$travelsArray = $this->pushToArray($this->travelsList(), ['leave_date','return_date'], $currentDate);
$trainingUsersArray = $this->pushToArray($this->trainingUsersList(), 'due_date', $currentDate);

Here is a sample of some of the code before I moved it to its own function.

        $separatedArray = $travelsArray = $trainingUsersArray = [];
        $currentDate = $date->format('Y-m-d');

        foreach($separatedList as $user) {
            if ($currentDate == $user->destroyed_date) {
                array_push($separatedArray, $user);
            }
        }

        foreach($travels as $travel) {
            if ($currentDate == $travel->leave_date || $currentDate == $travel->return_date) {
                array_push($travelsArray, $travel);
            }
        }
        foreach($trainingUsers as $trainingUser) {
            if ($currentDate == $trainingUser->due_date) {
                array_push($trainingUsersArray, $trainingUser);
            }
        }
  • 写回答

1条回答 默认 最新

  • douzhuo2002 2016-09-20 17:12
    关注

    Replace code of first if block with foreach loop.

    if (is_array($columnName)) {
        foreach ($columnName as $column) {
            if ($date == $item[$column]) {
                array_push($array, $item);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线