如何使用PHP检查数组中的键值是否存在


How to check key value is exist in an array using PHP

我需要检查一些键值是否存在于使用PHP的数组中。下面是我的代码:

$comment = json_encode(array(array('day_id' => '1', 'comment' => 'vodka0'),array('day_id' => '', 'comment' => ''), array('day_id' => '3', 'comment' => 'vodka3'),array('day_id'=>'4','comment'=>'hytt')));
$arrComment = json_decode($comment, true);

这里我需要检查一些day_id键有值或所有day_id键有空白值

使用array_columnarray_filter来检查:

// extract all day_id columns
$dayId = array_column($arrComment, 'day_id');
// filter the empty values
$filtered = array_filter($dayId);
if (empty($filtered)) {
  echo "All Day Ids are empty.";
}
else {
  echo "Some or all of them have some value.";
}

你的意思是:var_dump(array_column($arrComment, 'day_id'));

返回day_id键的所有值。

for ($i = 0; $i < count($arrComment); $i++) {
    if (isset($arrComment[$i]['day_id'])) {
        //value is set
    } else {
        //value is not set
    }
}