Search code examples
phpxmlconstantssuperglobals

Best practice to store arrays


I'm using this code to prepare values for meta data selection in posting and search forms.

<select>
<?php $my_custom_meta_regions = array (
 'region1' => 'Region1',
 'region2' => 'Region1', 
 'region3' => 'Region1'
 );
forearch ($my_custom_meta_regions as $key->$value) : ?>
  <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
?>

The problem is the code duplicates first, starts to be hard to manage and editing, growths in length. Can it be managed via xml, json, global constants, functions or class? Where is the best place to store this data and be accessed anywhere in script? I'm using this in wordpress theme.

I also tried this in functions.php , but I got a warning for undefined variable/constant when called.

//Registering the global constant in functions.php
<?php 
define('my_custom_meta_regions', serialize(array( 'region1','region2','region3' ) ) ); 
?>
//called in template
<?php $get_my_custom_meta_regions = unserialize(my_custom_meta_regions); ?>
// and then the warning appears

Sorry, if the question is too mainstream, but I can't come to solution, as far I use multiple of these structures for filling select fields.


Solution

  • I handled this by adding my array data as option in Wordpress using add_option inner function.