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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100