html表单数组如何存储到不包含null元素的php数组中

时间:2021-09-03 15:42:58

Storing an array submitted from forms stores elements with null values. Is there a way to store only non null fields into the php array?

存储从表单提交的数组存储具有空值的元素。有没有办法只将非空字段存储到php数组中?

$_SESSION['items'] = $_POST['items'];

$ _SESSION ['items'] = $ _POST ['items'];

is my current code.

是我目前的代码。

3 个解决方案

#1


You should take a look at array_filter(). I think it is exactly what you are looking for.

你应该看一下array_filter()。我认为这正是你要找的。

$_SESSION['items'] = array_filter($_POST['items']);

#2


# Cycle through each item in our array
foreach ($_POST['items'] as $key => $value) {
  # If the item is NOT empty
  if (!empty($value))
    # Add our item into our SESSION array
    $_SESSION['items'][$key] = $value;
}

#3


Like @Till Theis says, array_filter is definitely the way to go. You can either use it directly, like so:

就像@Till Theis所说,array_filter绝对是最佳选择。您可以直接使用它,如下所示:

$_SESSION['items'] = array_filter($_POST['items']);

Which will give you all elements of the array which does not evaluate to false. I.E. you'll filter out both NULL, 0, false etc.

这将为您提供不评估为false的数组的所有元素。 I.E.你会过滤掉NULL,0,false等。

You can also pass a callback function to create custom filtering, like so:

您还可以传递回调函数来创建自定义过滤,如下所示:

abstract class Util {
    public static function filterNull ($value) {
        return isset($value);
    }
}

$_SESSION['items'] = array_filter($_POST['items'], array('Util', 'filterNull'));

This will call the filterNull-method of the Util class for each element in the items-array, and if they are set (see language construct isset()), then they are kept in the resulting array.

这将为items-array中的每个元素调用Util类的filterNull方法,如果它们已设置(请参阅语言构造isset()),则它们将保留在结果数组中。

#1


You should take a look at array_filter(). I think it is exactly what you are looking for.

你应该看一下array_filter()。我认为这正是你要找的。

$_SESSION['items'] = array_filter($_POST['items']);

#2


# Cycle through each item in our array
foreach ($_POST['items'] as $key => $value) {
  # If the item is NOT empty
  if (!empty($value))
    # Add our item into our SESSION array
    $_SESSION['items'][$key] = $value;
}

#3


Like @Till Theis says, array_filter is definitely the way to go. You can either use it directly, like so:

就像@Till Theis所说,array_filter绝对是最佳选择。您可以直接使用它,如下所示:

$_SESSION['items'] = array_filter($_POST['items']);

Which will give you all elements of the array which does not evaluate to false. I.E. you'll filter out both NULL, 0, false etc.

这将为您提供不评估为false的数组的所有元素。 I.E.你会过滤掉NULL,0,false等。

You can also pass a callback function to create custom filtering, like so:

您还可以传递回调函数来创建自定义过滤,如下所示:

abstract class Util {
    public static function filterNull ($value) {
        return isset($value);
    }
}

$_SESSION['items'] = array_filter($_POST['items'], array('Util', 'filterNull'));

This will call the filterNull-method of the Util class for each element in the items-array, and if they are set (see language construct isset()), then they are kept in the resulting array.

这将为items-array中的每个元素调用Util类的filterNull方法,如果它们已设置(请参阅语言构造isset()),则它们将保留在结果数组中。