从数组php获得第一个非空值。
16lz
2021-01-22
If I have an array:
如果我有一个数组:
Array
(
[0] =>
[1] => a
[2] => b
[3] => c
)
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?
我想从数组中得到第一个非空值,在这里是a。我怎样才能轻松地做到呢?
6 个解决方案
#1
32
Not sure about nice and easy. But a short approach might be:
不太确定是否美好和轻松。但一个简短的方法可能是:
$first = current(array_filter($sparse_array));
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.
array_filter将提取“truthy”值,从而跳过空条目和假条目。而current只提供剩下的第一个条目。
更多相关文章
- 为什么foreach在循环中不修改数组时要复制数组?(复制)
- 学习成绩统计-【数组操作】
- PHP内爆不适用于大数组?
- 如何为$ wpdb-> insert创建一个包含column => value对的数组
- 如何在php中为json数组创建搜索功能
- json_encode在数组上返回null
- PHP数组值进入第二个数组[重复]
- 如何在php数组中插入新的键值对?
- mysql_fetch_array返回一个数组,数字为“1”