Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/135884-solved-forcing-a-format-to-an-array/
Share on other sites

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.  :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.