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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器