[英]Get list of index where their values are null in PHP
我有一個像
["21261","21262",null , null , "5000" , null , "5555"]
我需要得到一個索引列表,其中它們的值在 PHP 中為空,比如
[2,3,5]
謝謝
您可以將array_filter
用於數組過濾器元素的特定條件
$ar=["21261","21262",null , null , "5000" , null , "5555"];
$null=array_filter($ar,function($a){
return $a==null;
});
print_r(array_keys($null)); //[2,3,5]
<?php
// Your array
$a = ['test', null, 'hi', null];
// Empty array to push the keys
$a_null = [];
foreach($a as $key => $ele) {
// check if null
if($ele === null) {
// push the key
$a_null[] = $key;
}
}
// output keys
var_dump($a_null);
這應該是在不使用 inbuild php 函數的情況下理解它的最簡單方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.