How to check empty values ​​with in_array WITH EXCEPTIONS?

I am trying to figure out how to check for empty array values ​​with certain exceptions. Here is the array:

[name_first] => Name
[name_last] => 
[email] => blah@blah.com
[address] => 
[country] => USA

There are two empty values ​​- name_lastand address. Here is the code:

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

He will return falseas the [address]and [name_last]values empty. How can I ignore a specific key (say - [address])? It is generally assumed that it will look like this:

if (in_array(null, $data) **&& key_is_not('address', 'hatever')**) {
   echo 'empty values';
  }else{
     echo 'ok'; 
  }
+4
source share
6 answers

try the following:

$data = array('name_first' => "Name",
'name_last' => "",
'email' => "blah@blah.com",
'address' => "",
'country' => "USA");


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

Update

To exclude certain keys from verification, you can do this:

$data = array('name_first' => "",
'name_last' => "",
'email' => "blah@blah.com",
'address' => "",
'country' => "");

$array = array("name_first","country");
foreach ($data as $key => $value) {
  if($value=="" and (!in_array($key, $array)))
    echo "$key is Empty\n";
}
+1
source

This will check if the value is set and the length is greater than 0, and not an empty string.

foreach($elements as $key => $data)
{
    if(!in_array($key, ['address', 'something']))
    {
        if(count($data) > 0)
        {
            //stuff
        }
    }
}
+1
$input_array = [
    'name_first' => 'Name',
    'name_last' => '',
    'email' => 'blah@blah.com',
    'address' => '',
    'country' => 'USA',
    ];

, , address name_last

$ignore_search = in_array('', array_filter($input_array, function($k){
        return !in_array($k, ['address', 'name_last']);
    }, ARRAY_FILTER_USE_KEY));

array_filter , .

, , in_array array_search.

+1

, , , , , :

if (in_array_except(null, $data, array("address"))) {
    echo 'empty values';
}else{
    echo 'ok';
}

:

function in_array_except($needle, $haystack, $exception = array(), $strict = false) {
    if ($strict) {
        foreach($haystack as $needle_field => $item) {
            if (!in_array($needle_field, $exception) && $item === $needle) 
                    return true; 
        }
    } else {
        foreach($haystack as $needle_field => $item) {
            if (!in_array($needle_field, $exception) && $item == $needle) 
                    return true; 
        }
    }
    return false;
}

:

[name_first] => Name
[name_last] => 
[email] => blah@blah.com
[address] => 
[country] => USA

:

empty values
+1

array_search(), :

if (array_search("", $arr)) {
    echo 'empty values';
} else {
    echo 'ok'; 
}

** **

$array_var = array("11" => 111, "22" => 222, "33" => 333, "44" => 444, "55" => "", "66" => 666);
$new_array_var = array();

foreach ($array_var as $key => $value) {
//  echo $key . "==>" . $value . "<br/>";
    if (isset($value) && $value != "") {
        $new_array_var[$key] = $value;
    }
}

echo "<pre>";

echo "OLD : <br/>";
print_r($array_var);

echo "<br/><br/>";

echo "NEW : <br/>";
print_r($new_array_var);

echo "</pre>";
0
$ignore_array = array("address");
$blank_value_keys = array_keys($data,"");
if(sizeof($blank_value_keys)>0 && sizeof(array_diff($ignore_array,$blank_value_keys)>0){
     echo "Empty";
}else{
     echo "NOT Empty";
}

Add all the keys that you want to ignore in the list of ignored arrays. array_keyswill return all keys with empty values. Then we check to see if the array size is greater array_keys returned, greater than 0, and in $blank_value_keysmore than $ignore_arraymore than if the loop is executed. Note: array_diff will return all values ​​available in the second array, but not in the first

0
source

Source: https://habr.com/ru/post/1617174/


All Articles