【问题标题】:Output values of the array only if not empty or null仅当不为空或 null 时才输出数组的值
【发布时间】:2019-06-25 12:13:09
【问题描述】:

我真的是 php 新手,不知道我应该查找什么来解决这个问题。如果变量不为空也不为空,我试图只显示值。

在我分配的数组中:

$attributes [
    'glutenfree'     => getPublicClassificationsDescription($classifications, ARTICLE_GLUTENFREE),
    'lactosefree'    => getPublicClassificationsDescription($classifications, ARTICLE_LACTOSEFREE),
    'flavouringfree' => getPublicClassificationsDescription($classifications, ARTICLE_FLAVOURINGFREE),
    'corerange'      => getPublicClassificationsDescription($classifications, ARTICLE_CORERANGE),
    'engro'          => getPublicClassificationsDescription($classifications, ARTICLE_ENGRO),
    'vegan'          => getPublicClassificationsDescription($classifications, ARTICLE_VEGAN),
...
];

还有很多其他属性。 我想要的输出只有在不为空也不为空时才打印到 CSV。

现在我得到这样的结果:

glutenfree=,lactosefree=,flavouringfree=,corerange=,engro=,vegan=No,...

我需要的输出就像所有空/空的东西都应该消失,但有价值的东西应该在那里。在这个例子中:

vegan=No,...

例如,如果我尝试使用“empty”或“isset”,它不起作用,我得到一个没有错误的空白页。

$glutenfree = getPublicClassificationsDescription($classifications, ARTICLE_GLUTENFREE);

$attributes [
    if (!empty($glutenfree)) {
        'glutenfree'     => $glutenfree,
        'lactosefree'    => getPublicClassificationsDescription($classifications, ARTICLE_LACTOSEFREE),
        'flavouringfree' => getPublicClassificationsDescription($classifications, ARTICLE_FLAVOURINGFREE),
        'corerange'      => getPublicClassificationsDescription($classifications, ARTICLE_CORERANGE),
        'engro'          => getPublicClassificationsDescription($classifications, ARTICLE_ENGRO),
        'vegan'          => getPublicClassificationsDescription($classifications, ARTICLE_VEGAN),
        ...
    }
];

【问题讨论】:

    标签: php arrays if-statement isset is-empty


    【解决方案1】:

    在将数据推送到数组之前,您需要检查变量是否为空,如下所示:

    #first, create an empty array
    $attributes = array();
    #get lactose value
    $lactose_value = getPublicClassificationsDescription($classifications, ARTICLE_LACTOSEFREE);
    #check if not empty string
    if ($lactose_value !='') {
      #pushing to array
      $attributes['lactosefree'] = $lactose_value;
    }
    

    可以使用 foreach 指令改进此例程。

    $attributes = array()
    #all fields now are inside an array
    $fields = [ARTICLE_GLUTENFREE=>'glutenfree', ARTICLE_LACTOSEFREE=>'lactosefree',
              ARTICLE_FLAVOURINGFREE=>'flavouringfree', ARTICLE_CORERANGE=>'corerange' ,
              ARTICLE_ENGRO=>'engro', ARTICLE_VEGAN=>'vegan' ];
    #iterating
    $foreach($fields as $key=>$field) {
      #getting the value
      $arr_value = getPublicClassificationsDescription($classifications, $key);
      #check if not empty string
      if ($arr_value !='') {
        $attributes[$field] = $arr_value;
      }
    }
    

    感谢Dont Panic 的贡献。 谢谢mickmackusa的修改。

    【讨论】:

    • 我绝对同意第二位将是一个很好的改进,但我认为$fields 需要将问题中显示的原始$attributes 中的相同字符串键映射到这些常量,例如@ 987654327@ 等。我们真的不知道常量的值。
    • 好主意,我会改进这个答案。
    • 谢谢。它似乎有效,但我仍然得到一些空值,如 (glutenfree=, lactosefree=,)。也许它们没有设置(意味着它们是 NULL?)。是否也有解决方案?
    • @mickmackusa 感谢您的贡献。我添加了那些缺少的分号。我没有更改 !='' 因为在这种情况下我想检查一个空字符串,而不是一个空变量。
    • @matt 如果您var_dump($arr_value); 您将看到正在处理的元素的质量/长度。我应该直接问你,0 是一个可能的值吗?还是它们都是Yes/No 字符串?还是别的什么?
    【解决方案2】:

    最简单的解决方案(最简单的方法是对现有代码进行最少的修改)就是这样做

    $attributes = array_filter($attributes);
    

    在将数组转换为字符串之前。

    【讨论】:

    • ...假设您没有任何想要保留的零值。 array_filter() 有贪婪的倾向。 stackoverflow.com/a/43657546/2943403
    • 我同意@mickmackusa,这绝对是真的,在解决这个问题之前我已经写了一些答案,但在这种情况下,我认为它看起来不太可能。
    • 我无法验证所有有效值都是YesNo
    猜你喜欢
    • 1970-01-01
    • 2018-01-15
    • 2018-12-04
    • 2018-09-08
    • 2013-03-09
    • 2021-02-21
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    相关资源
    最近更新 更多