将具有空值的数组视为true' empty'用PHP

时间:2016-07-14 09:19:46

标签: php arrays

我有这个JSON数据:

public class NLService extends NotificationListenerService {
    private NLServiceReceiver nlservicereciver;
    Handler delayUpdateHandler = new Handler();
    private Runnable runBroadcastUpdate;

    public void triggerViewUpdate() {
        /* Accumulate view updates for faster, resource saving operation.
        Delay the update by some milliseconds.
        And if there was pending update, remove it and plan new update.
         */
        if (runBroadcastUpdate != null) {
            delayUpdateHandler.removeCallbacks(runBroadcastUpdate);
        }
        runBroadcastUpdate = new Runnable() {
            public void run() {
                // Do the work here; execution is delayed
            }
        };
        delayUpdateHandler.postDelayed(runBroadcastUpdate, 300);
    }

    class NLServiceReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            triggerViewUpdate();
        }
    }

}

解码后,变为:

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

当我尝试在PHP中使用empty()进行验证时,它仍会返回Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => ) 。我知道如果数组只是空数组true,PHP会接受该数组为FALSE

实际上我打算将这些空数组替换为空字符串。

如何将带有空字符串的数组视为'完全为空'阵列

感谢。

1 个答案:

答案 0 :(得分:2)

过滤

$array=array_filter($array);

如果没有提供任何进一步的选项,这将从数组中删除所有空元素,因此在这种情况下,您的数组将变为0长度,并且它将变为 true 为空,您正在寻找。

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

<强> Fiddle

如果您不想对原始数组进行任何更改,但只想检查所有值是否为空,则可以执行

var_dump(empty(array_filter($array))); // true. Original array remains same