我需要检查一些键值是否存在于使用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_column
和array_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
}
}