OldWolf Posted December 7, 2008 Share Posted December 7, 2008 This is going to be a strange question, but other than using a function that validates it, is there any way to force an array to fit a specific format? This job seems a bit small to create a class for, but maybe that's my best option? The job wouldn't be a whole lot more complicated than the example below. These individual arrays would be making up one larger array for a form... but I want each smaller array to have identical formats (I might not be the one writing every one of them). An example: I'd like to make sure that all the arrays have the following 5 keys (the real thing contains several more keys, some with their own arrays, but you get the point): Array( 'name' => '', 'type' => '', 'value' => '', 'required' => '', 'max_chars' => ''); Just to give background, all of the arrays are presently in an array, which are contained in an external file. The idea is that several files can reuse this same format, so that modifications are easy. Quote Link to comment https://forums.phpfreaks.com/topic/135884-solved-forcing-a-format-to-an-array/ Share on other sites More sharing options...
sKunKbad Posted December 7, 2008 Share Posted December 7, 2008 What do you mean by format? Do you want the array to be converted to XML or something? Quote Link to comment https://forums.phpfreaks.com/topic/135884-solved-forcing-a-format-to-an-array/#findComment-708362 Share on other sites More sharing options...
OldWolf Posted December 7, 2008 Author Share Posted December 7, 2008 Hehe, no, I wasn't very clear... sorry. What I mean is that I want specific keys to be the only ones in the array... no more nor less. Does that make more sense? Quote Link to comment https://forums.phpfreaks.com/topic/135884-solved-forcing-a-format-to-an-array/#findComment-708365 Share on other sites More sharing options...
JasonLewis Posted December 7, 2008 Share Posted December 7, 2008 Just make a function to check if all the correct keys are supplied. Shouldn't be to hard. Quote Link to comment https://forums.phpfreaks.com/topic/135884-solved-forcing-a-format-to-an-array/#findComment-708369 Share on other sites More sharing options...
OldWolf Posted December 7, 2008 Author Share Posted December 7, 2008 Thanks for your help. My solution came together like this: I have a class that stores the form already. I created a new method which accepts an array full of fields like described above. The method goes through each one, and sets it like so: $this->fields[$key] = array( 'type' => (isset($value['type'])) ? $value['type'] : 'none', 'lang' => (isset($value['lang'])) ? $value['lang'] : '', 'list_lang' => (isset($value['list_lang'])) ? $value['list_lang'] : '', And so forth. The ternary conditionals keep the code simple, but assure that each field as a value (either the default or the one passed), and no extra fields can be added because it only handles the ones I list. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/135884-solved-forcing-a-format-to-an-array/#findComment-708382 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.