phpmaster57 Posted February 15, 2016 Share Posted February 15, 2016 So I am looking at the code for https://github.com/vlucas/valitron/blob/master/src/Valitron/Validator.php a validation library, and currently trying to interpret the code. Most of it I understand but a part in the constructor is confusing me. It is as follows: public function __construct($data, $fields = array(), $lang = null, $langDir = null) { // Allows filtering of used input fields against optional second array of field names allowed // This is useful for limiting raw $_POST or $_GET data to only known fields $this->_fields = !empty($fields) ? array_intersect_key($data, array_flip($fields)) : $data; The part that is confusing me is the $this->_fields line, what exactly is the point of this and could you provide an example for when this when be needed? The part that really gets me is when it gets to array_intersect_key and array_flip, again what is the point of these? Quote Link to comment https://forums.phpfreaks.com/topic/300818-dont-understand-this/ Share on other sites More sharing options...
QuickOldCar Posted February 15, 2016 Share Posted February 15, 2016 ternary operator Same as an if/else, shortened Quote Link to comment https://forums.phpfreaks.com/topic/300818-dont-understand-this/#findComment-1531136 Share on other sites More sharing options...
Solution Jacques1 Posted February 15, 2016 Solution Share Posted February 15, 2016 The array_intersect_key() stuff is a very obscure way of removing all ($key, $value) pairs from $data where $key isn't in $fields. So $fields is basically a whitelist of allowed keys (as explained in the comments). Quote Link to comment https://forums.phpfreaks.com/topic/300818-dont-understand-this/#findComment-1531137 Share on other sites More sharing options...
QuickOldCar Posted February 15, 2016 Share Posted February 15, 2016 Not sure what I was thinking, maybe another coffee in order. Quote Link to comment https://forums.phpfreaks.com/topic/300818-dont-understand-this/#findComment-1531138 Share on other sites More sharing options...
phpmaster57 Posted February 15, 2016 Author Share Posted February 15, 2016 So what is the point of array_flip() on $fields, when you can pass in a normal array such as: ['username','pass','email'] to $fields. Quote Link to comment https://forums.phpfreaks.com/topic/300818-dont-understand-this/#findComment-1531140 Share on other sites More sharing options...
Jacques1 Posted February 15, 2016 Share Posted February 15, 2016 (edited) The point is to turn the array values of $fields into keys so that they can be passed to array_intersect_key(): <?php $fields = ['username', 'pass', 'email']; var_dump($fields); $flipped_fields = array_flip($fields); var_dump($flipped_fields); Using $fields directly wouldn't work, because it has numerical keys which aren't very useful here. Edited February 15, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/300818-dont-understand-this/#findComment-1531141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.