簡體   English   中英

獲取在 PHP 中其值為空的索引列表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM