简体   繁体   中英

Check if all items of array are empty

I'm beginner in the PHP programming. Please can somebody suggest me any trick? How could I shorten this script with any cycle function?

$org[1] = $_POST['name'];
$org[2] = $_POST['street'];
$org[3] = $_POST['city'];
$org[4] = $_POST['postal_code'];
$org[5] = $_POST['org_nmr'];
$org[6] = $_POST['tin'];

if (
    (empty($org[1])) 
    && (empty($org[2])) 
    && (empty($org[3])) 
    && (empty($org[4])) 
    && (empty($org[5])) 
    && (empty($org[6]))
) {
    echo 'Not enter have no data';
}

I think that it is possible to make it somehow. But I don't know how.

This simple answer is tailored to user's example question (it does not take into account arrays whose values are arrays/objects etc).

$data = trim(implode('',$org));
if (strlen($data) === 0){ 
 ... 
}

I changed from empty($data) to strlen($data) === 0 in if condition in case that having value zero is a non empty situation for you; because as we know empty($data) will be true when $data = '0')

You can avoid repetition with a loop. It might also make more sence to keep the named associative array rather than a numerically indexed one for $org .

$org = array();
foreach($_POST as $name => $value) {
  If (! empty($value)) {
    $org[$name] = $value;
  }
}
If (empty($org)) { //... Error here

You should do that check before you access any Key in the POST array.

Required no loop at all, that foreach is just used to output missing keys, not to do the check

<?php
$required = array_flip(array('key_a', 'key_b')); // list of required keys, just used array_flip for ease of writing
$post = array('key_a' => 'a value'); // your $_POST array
$check = array_diff_key($required, $post);
if(!empty($check))  {
   // foreach not required as answer to your question, just to point missing keys out.
   foreach($check as $key => $value) {
       echo $key, 'key is missing!', PHP_EOL;
   }
   die('Some message');
}

you can use array_filter built in function,

if(count(array_filter($_POST)) == 0)
   echo 'Nezadal(a) jste žádné údaje';
$values = array('name', 'street', 'city', 'postal_code', 'org_nmr', 'tin');
$getLength = function($val, $post_data){
    return is_null($post_data[$val]) ? 0 : strlen($post_data[$val]);
};
if(array_sum(array_map($getLength, $values, $_POST)) == 0){
    echo 'Nezadal(a) jste žádné údaje';
}

Basically you create a function which will determine the length of an array element and return it's length.

You then map this function to all all elements and then sum it.

surprised nobody mentioned in_array ?

if (in_array("", (array) $org)) {
   echo "something is empty in this array";
}

A take on the person's above, I would cast $org perhaps :

$test = trim(implode('', (array) $org));
if (strlen($test) === 0){ 
   ... 
}

More examples:

$arr0 = array('Hello', 'World!', 'Beautiful', 'Day!');
$arr1 = array("something" => 'here');
$arr2 = array("something" => '');
$arr3 = array();
$arr4 = "string";
$arr5 = 34;
$arr6 = '';

$test = trim(implode('', (array)$arr0));
if (strlen($test) === 0) {
    echo "No data in this array";
} else {
    echo "Data in this array \n";
}


$test = trim(implode('', (array)$arr1));
if (strlen($test) === 0) {
    echo "No data in this array\n";
} else {
    echo "Data in this array \n";
}

$test = trim(implode('', (array)$arr2));
if (strlen($test) === 0) {
    echo "No data in this array\n";
} else {
    echo "Data in this array \n";
}

$test = trim(implode('', (array)$arr3));
if (strlen($test) === 0) {
    echo "No data in this array\n";
} else {
    echo "Data in this array \n";
}

$test = trim(implode('', (array)$arr4));
if (strlen($test) === 0) {
    echo "No data in this array\n";
} else {
    echo "Data in this array \n";
}

$test = trim(implode('', (array)$arr5));
if (strlen($test) === 0) {
    echo "No data in this array\n";
} else {
    echo "Data in this array \n";
}


$test = trim(implode('', (array)$arr6));
if (strlen($test) === 0) {
    echo "No data in this array\n";
} else {
    echo "Data in this array \n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM