The add_option, get_option, and update_option functions accept values as single variables or arrays of data. When given an array, they transform the information received to a serialized array that gets stored in the site database. The main advantage of using arrays over multiple options is that all of the information can be retrieved with a single function call, optimizing the access to the MySQL database and simplifying your plugin's code. This is especially important when your plugin options need to be queried every time a site page needs to be rendered.
Of course, this advantage is only true if you need to use most plugin options at the same time. Otherwise, your code will be managing large amounts of data for no reason.
Another benefit of using arrays instead of individual options is that the names of each option can be much shorter and simpler, since you only need to worry about avoiding naming conflicts at the top option name level, as opposed to each key in the array. Finally, having all the options stored in a single array makes the bulk removal of these options much easier than if they were all stored separately, as we will see in the next recipe.
The bulk of this recipe's code defines a utility function called ch2pho_get_options, which is used to make sure that we always get good values when retrieving options, even when our plugin runs for the first time or new options are introduced by an upgrade. As part of this utility function, we use the wp_parse_args function to quickly compare an existing set of options retrieved by the get_option function with the current set of default plugin options, specified by the $new_options array. For each array element that is not found in the existing options, wp_parse_args will simply merge it into the resulting array, which is returned at the end of the function. The last part of our option-retrieval function checks whether the previous option array was empty or whether any new keys were added to the new array, using the PHP array_diff_key function. In either of these cases, it will save the updated options array back to the site database.
It should finally be noted that, while the ch2pho_get_options function returns an array of all the site options, we are not actually using this return value in this recipe; we will use it in later recipes in this chapter.