4

I have used variable_set to save one value. How can I save multiple values through an array using variable_set?

11

It's the same:

The variable_set function saves the variable to database serializing it. variable_get takes the variable from $conf where the variable is already unserialized.

Example:

$var1 = array(
 'key1' => 'value1',
 'key2' => 'value2'
);
variable_set('var1', $var1);
print_r variable_get('var1');

Output

Array
(
    [key1] => value1
    [key2] => value2
)
  • Yes, this is the way to do it, just pass an array to variable_set. Although perhaps a consequence of brevity and example, do remember to pass a default value of the expected data type to variable_get as the second parameter, e.g $var1 = variable_get('var1', array()); – David Thomas Oct 10 '14 at 8:39
  • the second parameter in variable_get is optional on D7. – jorgetutor Oct 10 '14 at 9:04
  • 1
    Optional, but recommended. For example, imagine you're going to foreach over that variable and it's empty for some reason, you would then foreach over NULL and trigger a PHP notice error whereas passing array() as the expected default value data type will avoid such an error. – David Thomas Oct 10 '14 at 9:09
1

If you wand do it in the system form, you can use #tree param.

    $settigs = variable_get('default_settings', array(
      'setting_1' => NULL,
      'setting_2' => NULL,
    ));
    $form['default_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Default settings'),
      '#weight' => 5,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE
    );
    $form['default_settings']['setting_1'] = array(
      '#type' => 'textfield',
      '#title' => t('Setting 1'),
      '#default_value' => $settings['setting_1'],
      '#required' => TRUE
    );
    $form['default_settings']['setting_2'] = array(
      '#type' => 'textfield',
      '#title' => t('Setting 2'),
      '#default_value' => $settings['setting_2'],
      '#required' => TRUE
    );
  • The OP is asking how to save an array with variable_set(). Your answer doesn't explain that. since using that code would still require you to call variable_set(). – kiamlaluno Mar 14 '16 at 8:46
0

I think best way to save array value in varible_set(); is json. drupal_json_encode() and drupal_json_decode() function in drupal 7.

You can do something like this:-

$var_input = array(
 'key1' => 'value1',
 'key2' => 'value2'
);
$var_input = json_encode($var_input);  // convert to json

variable_set('var1', $var_input); // set in variable

$var_output = variable_get('var1'); // get from variable

$var_output = drupal_json_decode($var_output); // decode json

print_r($var_output);

Hope this help!

  • 3
    No need to save as JSON, especially when variable_get is called and used in PHP just set the variable with variable_set and pass an array. That way, an array will be returned with variable_get automatically ready for PHP. – David Thomas Oct 10 '14 at 8:38
0

For variable_set() there isn't any difference between saving a persistent variable containing a string or an array, since it passes its $value parameter to serialize().

db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();

Eventually, what changes for you is what you do when the saved value comes from a form. To make it easier for you to save an array, you could use a form with the following structure.

  $options = variable_get('admin_theme', array());
  $form['admin_theme'] = array(
    '#type' => 'fieldset',
    '#title' => t('Administration theme'),
    '#tree' => TRUE,
  );
  $form['admin_theme']['admin_theme'] = array(
    '#type' => 'select',
    '#options' => array(0 => t('Default theme')) + $theme_options,
    '#title' => t('Administration theme'),
    '#description' => t('Choose "Default theme" to always use the same theme as the rest of the site.'),
    '#default_value' => $options['admin_theme'],
  );
  $form['admin_theme']['node_admin_theme'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use the administration theme when editing or creating content'),
    '#default_value' => $options['node_admin_theme'],
  );

In the submission handler, you can then save the settings with variable_set('admin_theme', $form_state['values'][admin_theme']).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.