WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to check if variable is empty in multi-array

[Resolved] How to check if variable is empty in multi-array

  • Josh
    Participant

    @josh401

    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Josh
    Participant

    @josh401

    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.

    Josh
    Participant

    @josh401

    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;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] How to check if variable is empty in multi-array’ is closed to new replies.