Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for empty values with in_array WITH EXCEPTIONS?

Tags:

arrays

php

I am trying to figue out how to check for empty values of an array with certain exceptions. Here is the array:

[name_first] => Name
[name_last] => 
[email] => [email protected]
[address] => 
[country] => USA

There are two empty values - name_last & address. Here is the code:

if (in_array(null, $data)) {
   echo 'empty values';
  }else{
     echo 'ok'; 
  }

It will return false as [address] and [name_last] values are empty. How can I ignore a particular key (let's say - [address])? Basically it is supposed to LOOK like this:

if (in_array(null, $data) **&& key_is_not('address', 'hatever')**) {
   echo 'empty values';
  }else{
     echo 'ok'; 
  }
like image 430
Rossitten Avatar asked Nov 24 '15 05:11

Rossitten


1 Answers

try this :

$data = array('name_first' => "Name",
'name_last' => "",
'email' => "[email protected]",
'address' => "",
'country' => "USA");


foreach ($data as $key => $value) {
  if($value=="")
    echo "$key is Empty\n";
}

Update

To exclude particular keys from the check, you can do this way :

$data = array('name_first' => "",
'name_last' => "",
'email' => "[email protected]",
'address' => "",
'country' => "");

$array = array("name_first","country");
foreach ($data as $key => $value) {
  if($value=="" and (!in_array($key, $array)))
    echo "$key is Empty\n";
}
like image 122
AkshayP Avatar answered Sep 30 '22 04:09

AkshayP