Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:45:08+00:00 2026-06-14T20:45:08+00:00

Sorry for the terrible headline, let me try to explain below. I have written

  • 0

Sorry for the terrible headline, let me try to explain below.

I have written a bunch of small functions that either returns true or false.

validateName()
validateEmail()
validateAddr()
validateBirtd()
validateUsername()

Now I am looping through a lot of data imported with a CSV file, and checkin which data is valid or not (returns true or false).

I do it this way:

if (validateName($data[0]) == true AND validateEmail($data[1]) == true AND validateAddr($data[3]) == true AND validateBirtd($data[5]) == true AND validateUsername($data[6])==true) {
 // create array to import etc etc
}else{
 // create other array with data who failed validation, to show user later..etc etc
}

My question is – is there a more clever way to do this? Would it be possible to create a list of for each failed validation ? Say 3 entrys has fails the validateEmail() function, and 10 both fails validateEmail and validateName().

Would there be a way for me to sort this so I can tell the user “these entrys failed email validation” and “these entrys failed Name and email validation”.

I thought about validating one field at a time, but this way I would have duplicates if one entry has more than one validation error.

Would be cool if there was some kind of logic that I don’t know of where I could do this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:45:09+00:00Added an answer on June 14, 2026 at 8:45 pm

    You can use Iterators to get CSV content and filter at the same time. you can also add different callback to each CSV index

    Example

    $csv = new CSVFilter(new CSVIterator("log.txt"));
    $csv->addFilter(0, "validateName"); //<------------ Means validate Index at 0
    $csv->addFilter(1, "validateEmail");
    $csv->addFilter(2, "validateAddr");
    $csv->addFilter(3, "validateBirtd");
    $csv->addFilter(4, "validateName");
    $csv->addFilter(5, "validateUsername");
    
    foreach ( $csv as $data ) {
        var_dump($data);
    }
    
    //To get Errors 
    var_dump($csv->getErrors());
    

    CSV Filter

    class CSVFilter extends FilterIterator {
        protected $filter = array();
        protected $errors = array();
    
        public function __construct(Iterator $iterator) {
            parent::__construct($iterator);
        }
    
        public function addFilter($index, Callable $callable) {
            $this->filter[$index] = $callable;
            $this->errors[$callable] = 0;
        }
    
        public function getErrors() {
            return $this->errors;
        }
    
        public function accept() {
            $line = $this->getInnerIterator()->current();
            $x = true;
            foreach ($this->filter  as $key => $var ) {
                if (isset($line[$key])) {
                    $func = $this->filter[$key];
                    $func($var) or $this->errors[$func] ++ and $x = false;
                }
            }
            return $x;
        }
    }
    

    CSVIterator

    class CSVIterator implements \Iterator {
        protected $fileHandle;
        protected $line;
        protected $i;
    
        public function __construct($fileName) {
            if (! $this->fileHandle = fopen($fileName, 'r')) {
                throw new \RuntimeException('Couldn\'t open file "' . $fileName . '"');
            }
        }
    
        public function rewind() {
            fseek($this->fileHandle, 0);
            $this->line = fgetcsv($this->fileHandle);
            $this->i = 0;
        }
    
        public function valid() {
            return false !== $this->line;
        }
    
        public function current() {
            return array_map("trim", $this->line);
        }
    
        public function key() {
            return $this->i;
        }
    
        public function next() {
            if (false !== $this->line) {
                $this->line = fgetcsv($this->fileHandle);
                $this->i ++;
            }
        }
    
        public function __destruct() {
            fclose($this->fileHandle);
        }
    }
    

    Simple Random Functions

    function validateName($var) {
        return mt_rand(0, 5);
    }
    
    function validateEmail($var) {
        return mt_rand(0, 5);
    }
    
    function validateAddr($var) {
        return mt_rand(0, 5);
    }
    
    function validateBirtd($var) {
        return mt_rand(0, 5);
    }
    
    function validateUsername($var) {
        return mt_rand(0, 5);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the terrible title. I have a signed assembly 'Signed.dll' that is a
I am sorry for the terrible description of this question. I am making a
Sorry I am very new to open graph and have been having difficulty to
Sorry for the terrible title, best I could think of at the time! Say
Sorry about terrible title, not sure of a better one. Here's a gross simplification
Sorry for how stupid this is going to sound. My JS vocabulary is terrible
Okay I have the below code performing something I don't want it to do.
I'm trying to write a small Python module which contain some mathematical functions. For
Sorry for the terrible Question Title, I don't know how to phrase this, feel
I have created a service in Win7 using C#. Within that service I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.