Ok, I have a pretty customized question so bear with me.
I basically have two sets of data that I want to compare with a 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 is INVALID because it is missing (Rule 3)
);
$required = array(
'color'=>array('white','blue'),
'heavy'=> 'no',
'name'
);
$errors = array(
'color'=>array('required'=>'Color is Required','invalid'=>'Color invalid')
'object'=>array('invalid'=>'Object invalid'),
'texture'=>array('invalid'=>'Texture invalid'),
'heavy'=>array('required'=>'Heavy is Required','invalid'=>'Heavy invalid'),
'name'=>array('required'=>'Name is 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 I want to do is run $data
through the $blueprint
and make sure of the following:
- If the
$data
key/value pair matches a$blueprint
key/value pair,$data
's k/v is valid - If the
$data
key/value pair matches a$blueprint
key and a value from the nested array,$data
's k/v is valid - If the
$data
array omits a key/value pair which exists in$blueprint
,$data
's k/v may still be valid if it is not located in the$required
array - If the
$data
array supplies a key/value pair which does not exist in$blueprint
,$data
's k/v is invalid - If the
$data
key from a key/value pair matches a$blueprint
value without a defined key,$data
's k/v can still be valid. However, if the$blueprint
has both a key and value defined,$data
's k/v must meet the requirements of rule 1 to be valid. - I'd like to impose a character limit on several of the
$blueprint
k/v where if a$data
's k/v exceeds this character limit,$data
s k/v is not valid
If a $data
's k/v is invalid, I'd then like to somehow associate an error with that particular k/v describing why it is invalid (surpassed character limit, general error etc.) Perhaps the error would be defined in a third array?
I've looked into array_intersect_assoc
but not sure if this is beyond the scope of that function. Also, there will be a good amount of values in the $blueprint
, so I need something as versatile as possible.
I think that this is right, my brain sort of melted while I was writing this, so please don't hesitate to ask if confused. Am I better off just validating each k/v individually?
Let's see who is the brainiac out there.