How can I get my options in json response?

I have made a function to download a JSON file with all the options in WordPress and my own options that I have made in my theme options page and it works fine.

Here is the code:

$options = get_alloptions(); // Get all options data, return array

    foreach ($options as $key => $value) {
        $value = maybe_unserialize($value);
        $need_options[$key] = $value;
    }

$json_file = json_encode($need_options);

So what’s the problem:

The problem is in the downloaded file there are too many options I want to limit them means I want only the options that are made by me in my theme options.

Solutions Collecting From Web of "How can I get my options in json response?"

You shouldn’t save your theme’s options one by one. In that case, you should call each of them separately, or store them in an array and then run a loop to get their values:

$options = array( 'option1', 'option2', 'option3');
foreach( $options as $option ){
    // The rest of your code
}

But the proper way would always be to serialize and store all of your theme’s options in a single option. Take a look at this answer of mine, which you can find the functions to do this in the code.

Another approach would be to use a unique prefix for your options. For example, use something- as a prefix, and then check the retrieved option for that string:

$options = get_alloptions(); // Get all options data, return array

    foreach ($options as $key => $value) {
        if( stristr( $key, 'something-' ) )  {
            $value = maybe_unserialize($value);
            $need_options[$key] = $value;
        }
    }

$json_file = json_encode($need_options);