dtrj74376 2016-07-14 09:19
浏览 31
已采纳

在PHP中将具有空值的数组视为真正的“空”

I have this JSON data:

["","","","","","",""]

When decoded, it turns into:

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => )

When I try to validate using empty() in PHP, it still returns true. I aware that PHP will accept that array as FALSE if it is only empty array: Array().

Actually I intended to replace those empty arrays into an empty string.

How to treat that array with empty string as 'totally empty' array?

Thanks.

  • 写回答

1条回答 默认 最新

  • doutao5499 2016-07-14 09:21
    关注

    Filter it

    $array=array_filter($array);
    

    Without providing any further options, this will remove all empty elements from the array hence your array will become 0 length in this case and it will become true empty that you are looking for.

    $array=json_decode('["","","","","","",""]');
    $array=array_filter($array);
    var_dump(empty($array));  // true
    

    Fiddle

    And if you don't want to make any changes to the original array but just want to check if all values are empty you can do

    var_dump(empty(array_filter($array))); // true. Original array remains same
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?