satya satya - 11 months ago 41
PHP Question

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

I need one help. I need to check some key value is exist in an array using PHP. I am explaining my code below.

$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);

Here i need to check some
key has value or all
key has blank value.Please help.

Answer Source

Use array_column and array_filter to check this:

// 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.";