WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to check if variable is empty in multi-array (3 posts)

  1. Hi,

    I am trying to check if an option in the wp_options table exists. If it does, I would like to update the info. If it does not, I would like to leave it alone.

    Here is what I am using to get the info:

    $opts3 = get_option('jwl_options_group');
    $opts2 = $opts3['jwl_ww_options']['date_installed'];

    And here is my test:

    if (!$opts2) {
       $opts4 = get_option('jwl_options_group');
       $opts4['jwl_ww_options']['date_installed'] = strtotime('now');
       update_option('jwl_options_group', $opts4);
       return;
    }

    However, every time the page loads, it is updating the timestamp. Regardless of whether this option is already set or not.

    Any suggestions?

  2. Update:

    It will also knock out whatever options are already being stored in the jwl_options_group array in the database. It replaces any contents in this array with just the serialized timestamp... instead of updating the array ONLY IF the array doesn't exist.

  3. Got it.

    In case anyone is wondering. You must use the isset function to check if the value exists.

    This works:

    $opts3 = get_option('jwl_options_group');
    $opts2 = isset($opts3['jwl_ww_options']['date_installed']);
    if (!$opts2) {
       $opts4 = get_option('jwl_options_group');
       isset($opts4['jwl_ww_options']['date_installed']) = strtotime('now');
       update_option('jwl_options_group', $opts4);
       return;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.