得到的PHP数组第一个非空值 [英] get first non-null value from array php

查看:396
本文介绍了得到的PHP数组第一个非空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个数组:

Array
(
    [0] => 
    [1] => a
    [2] => b
    [3] => c
)

和我想从阵列得到的第一个非空值,在这种情况下,A。我怎么能去这样做很好的和容易吗?

And I want to get the first non-null value from the array, in this case "a". How could I go about doing that nice and easily?

推荐答案

不知道有关好和容易。但短短的方法可能是:

Not sure about nice and easy. But a short approach might be:

 $first = current(array_filter($sparse_array));

array_filter 将提取您在 truthy 的价值观,因此跳过空,假的条目。虽然 电流 只是给你的第一个剩下的那些条目。

Where array_filter will extract you the "truthy" values, thus skipping empty and false entries. While current simply gives you the first of those remaining entries.

这篇关于得到的PHP数组第一个非空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆