PHP Compare Array Values for Validation -
ok, have pretty customized question bear me.
i have 2 sets of data want compare lot of different possibilities.
$data = array( 'object'=>'ball', // should check valid (rule 2) 'color'=>'white', // valid (rule 2) 'heavy'=>'no', // valid (rule 1) 'name'=>'wilson', // valid (rule 5) 'funny'=>'no' // invalid (rule 4) ); $data_2 = array( 'object'=>'box', // valid (rule 2) 'color'=> 'blue', // valid (rule 2) 'texture'=>'hard', // valid (rule 1) 'heavy'=>'yes', // invalid (rule 4) 'stupid'=>'no' // invalid (rule 4) // name invalid because missing (rule 3) ); $required = array( 'color'=>array('white','blue'), 'heavy'=> 'no', 'name' ); $errors = array( 'color'=>array('required'=>'color required','invalid'=>'color invalid') 'object'=>array('invalid'=>'object invalid'), 'texture'=>array('invalid'=>'texture invalid'), 'heavy'=>array('required'=>'heavy required','invalid'=>'heavy invalid'), 'name'=>array('required'=>'name required','max_char'=>'name exceeds char limit', 'invalid'=>'invalid item provided', ); $blueprint = array( 'object'=>array('box','ball'), 'color'=>array('blue','white'), 'texture'=>'hard', 'heavy'=>'no', 'name' );
what want run $data
through $blueprint
, make sure of following:
- if
$data
key/value pair matches$blueprint
key/value pair,$data
's k/v valid - if
$data
key/value pair matches$blueprint
key , value nested array,$data
's k/v valid - if
$data
array omits key/value pair exists in$blueprint
,$data
's k/v may still valid if not located in$required
array - if
$data
array supplies key/value pair not exist in$blueprint
,$data
's k/v invalid - if
$data
key key/value pair matches$blueprint
value without defined key,$data
's k/v can still valid. however, if$blueprint
has both key , value defined,$data
's k/v must meet requirements of rule 1 valid. - i'd impose character limit on several of
$blueprint
k/v if$data
's k/v exceeds character limit,$data
s k/v not valid
if $data
's k/v invalid, i'd somehow associate error particular k/v describing why invalid (surpassed character limit, general error etc.) perhaps error defined in third array?
i've looked array_intersect_assoc
not sure if beyond scope of function. also, there amount of values in $blueprint
, need versatile possible.
i think right, brain sort of melted while writing this, please don't hesitate ask if confused. better off validating each k/v individually?
let's see brainiac out there.
i feel sort of silly, here's brute force method. #6 free because it's not 'in' array in sense.
foreach ($data $k => $v) { if (empty($blueprint[$k])) { // (3) data defines key isn't defined in blueprint. } else { if (is_array($blueprint[$k])) { if (in_array($v, $blueprint[$k])) { // (2) data defines valid value in blueprint list. } else { // (also 4) data defines value not in blueprint list. } } else if ($v == $blueprint[$k]) { // (1) data defines value in blueprint. } else if (in_array($v, $blueprint)) { // (5) data in blueprint without key. } else { // (4) data invalid. } } }
edit: loop checking if $blueprint has key $data doesn't define. there should toggle make sure @ necessary (in previous block) before running it.
foreach ($blueprint $k => $v) { if (empty($data[$k])) { // (6) data doesn't have required key blueprint. } }
Comments
Post a Comment