I got the following array
(
[0] => 1
[1] => John Doe
[2] => john
[3] => [email protected]
[4] => lorem
[5] => Lorem, Ipsum Dolor Sit Amet
)
Basically I am reading those values from CSV file, I want to validate the data before inserting into the database, here is the basic validation i want to apply
- Check if array contains 6 elements
- All fields are required and not empty
Here is what i am doing:
if (count($value) == 6) {
$params = array(
'id' => array_key_exists(0, $value) && !empty($value[0]) ? $value[0]: null,
'name' => array_key_exists(1, $value) && !empty($value[1]) ? $value[1]: null,
'username' => array_key_exists(2, $value) && !empty($value[2]) ? $value[2]: null,
'email' => array_key_exists(3, $value) && !empty($value[3]) ? $value[3]: null,
'password' => array_key_exists(4, $value) && !empty($value[4]) ? $value[4]: null,
'position' => array_key_exists(5, $value) && !empty($value[5]) ? $value[5]: null
);
}
I am wondering, what would be the better way of handling this? what i don't like is the repetition, Probably i can solve by putting it inside a loop, I want to know from you how would you do it?
Thanks.