【问题标题】:Include array into function only if it exist and is not empty仅当数组存在且不为空时才将其包含到函数中
【发布时间】:2019-07-08 20:07:32
【问题描述】:

我有一个函数可以打乱几个数组并返回一个长数组:

function array_zip_merge() {
  $output = array();
  // The loop incrementer takes each array out of the loop as it gets emptied by array_shift().
  for ($args = func_get_args(); count($args); $args = array_filter($args)) {
    // &$arg allows array_shift() to change the original.
    foreach ($args as &$arg) {
      $output[] = array_shift($arg);
    }
  }
  return $output;
}

我是这样运行的:

$visirezai = array_zip_merge($tretiRezai, $ketvirtiRezai, $sphinxorezaiclean);

问题有时是一个、两个甚至所有数组都是空的或根本没有设置,我收到这样的循环错误消息:

Notice: Undefined variable: sphinxorezaiclean in /usr/share/nginx/search.php on line 177

Warning: array_shift() expects parameter 1 to be array, boolean given in /usr/share/nginx/search.php on line 148

Warning: array_shift() expects parameter 1 to be array, boolean given in /usr/share/nginx/search.php on line 148

Warning: array_shift() expects parameter 1 to be array, null given in /usr/share/nginx/search.php on line 148

Warning: array_shift() expects parameter 1 to be array, boolean given in /usr/share/nginx/search.php on line 148

Warning: array_shift() expects parameter 1 to be array, boolean given in /usr/share/nginx/search.php on line 148

Warning: array_shift() expects parameter 1 to be array, boolean given in /usr/share/nginx/search.php on line 148

第 177 行是 $visirezai = array_zip_merge($tretiRezai, $ketvirtiRezai, $sphinxorezaiclean); 所在的位置(我知道 sphinxorezaiclean 根本没有设置,但有时是这样)和第 148 行 - 函数 array_zip_merge 所在的位置。

它会继续这样,直到我停止在浏览器中加载网页。

我解决这个问题的方法是这样的:首先我检查数组是否为空:

$ketvirtiRezai = rezultataiKeturi($q);
$tretiRezai = rezultataiTrys($q);

$ketvirtiEmpty = false;
$tretiEmpty = false;
$sphinxEmpty = false;
if (empty($ketvirtiRezai[0])) {
    $ketvirtiEmpty = true;
}

if (empty($tretiRezai[0])) {
    $tretiEmpty = true;
}
else {
    $tretiRezai = array_slice($tretiRezai, 0, 5);
}

if (isset($sphinxorezai) && !empty($sphinxorezai)) {
    $sphinxorezaiclean = array_slice($sphinxorezai, 0, 5);
}
else
{
    $sphinxEmpty = true;
}

然后我可以通过检查每个数组的真或假并相应地设置array_zip_merge 函数来执行超长 if elseif 循环。

有没有更好的方法来添加/删除数组到 array_zip_merge 函数。例如,如果 $ketvirtiRezai 是空的,函数应该只包含 $visirezai = array_zip_merge($tretiRezai, $sphinxorezaiclean); 如果所有数组都是空的,那么 $visirezai 应该设置为空并且函数根本不运行(我猜这个很容易)。如果两个数组中的一个不是空的,$visirezai 应该被设置为一个不为空的。

我是 PHP 新手,很抱歉我的代码乱七八糟。

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    我想你只需要使用php函数is_array,对吧?在您的合并功能中:

    function array_zip_merge() {
      $output = array();
      // The loop incrementer takes each array out of the loop as it gets emptied by array_shift().
      for ($args = func_get_args(); count($args); $args = array_filter($args)) {
        // &$arg allows array_shift() to change the original.
        foreach ($args as $key=>&$arg) {
          // check if the argument is actually an array
          if (is_array($arg)) {
            $output[] = array_shift($arg);
          } else {
            unset($args[$key]);
          }
        }
      }
      return $output;
    }
    

    【讨论】:

    • 真是个好主意,但是当我检查 is_array 时,我得到了这个错误:致命错误:在第 147 行的 /usr/share/nginx/search.php 中超过了 30 秒的最大执行时间,所以它只是挂起在那个函数执行上。第 147 行是这个循环开始的地方 foreach ($args as &$arg) {
    • @DadaB,如果你添加一个!empty 检查:if (!empty($arg) && is_array($arg)) {
    • 试试@MER 的建议,也试试var_export($args); 看看那里发生了什么。
    • 如前所述,@MER 解决方案仍然出现执行时间错误。 Var_export 给了我这个:array ( 0 => true, 1 => true, 2 => '', )array ( 0 => true, 1 => true, )array ( 0 => true, 1 => true, )数组 ( 0 => true, 1 => true, )array ( 0 => true, 1 => true, )array ( 0 => true, 1 => true, )array ( 0 => true, 1 => true , )array ( 0 => true, 1 => true, )array ( 0 => true, 1 => true, )array ( 0 => true, 1 => true, )array ( 0 => true, 1 = > true, )array ( 0 => true, 1 => true, )array ( 0 => true, 1 => true, )array ( 0 => true, 1 => true, ) 并且它继续这样不停.
    • 我会查看生成发送到函数的数组的代码。你传递了多少个参数?
    猜你喜欢
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2017-09-16
    • 2018-01-15
    • 2012-12-26
    • 1970-01-01
    相关资源
    最近更新 更多