0

I have PHP array:

$curl_options = array(
    CURLOPT_PORT => 80,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30
);

Then I add new elements and change some values:

$curl_options[CURLOPT_USERAGENT] = "Opera/9.02 (Windows NT 5.1; U; en)";

$curl_options[CURLOPT_PORT] = 90;

After this changes array becomes to

$curl_options = array(
    CURLOPT_PORT => 90,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_USERAGENT => Opera/9.02 (Windows NT 5.1; U; en)
);

How can I reset array back to it defaults? To

$curl_options = array(
    CURLOPT_PORT => 80,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30
);

Thanks.

1
  • Tell us what you're exactly trying to do here, and maybe we could provide you with the solutions that fit you. Note that all of these answers work, and they're all really simple. So let me conclude that your question can't be that simple, and you need some other workaround. But if your question really is that simple, please choose one as an answer :) Commented May 12, 2012 at 19:01

4 Answers 4

2

The "true" way is create a function getDefaultOptions which returns needed array.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to make a copy of the array:

$curl_options = array(
CURLOPT_PORT => 80,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30);

$copy = $curl_options;

$curl_options[CURLOPT_USERAGENT] = "Opera/9.02 (Windows NT 5.1; U; en)";
$curl_options[CURLOPT_PORT] = 90;

// Reset
$curl_options = $copy;

Comments

2

The only way to do this is to overwrite the array with its original, so just run this again:

$curl_options = array(
CURLOPT_PORT => 80,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30);

PHP doesnt store any revision data or something like that, so you cant reverse array changes.

Comments

0

Make 2 separate arrays - 1) Default 2) Extension.

$curl_options_default = array(
    CURLOPT_PORT => 80,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30
);

$curl_options[CURLOPT_USERAGENT] = "Opera/9.02 (Windows NT 5.1; U; en)";
$curl_options[CURLOPT_PORT] = 90;

$curl_options_new = array_replace($curl_options_default, $curl_options);

Now you have 2 arrays: untouched $curl_options_default and new (with extended/replaced elements) $curl_options_new

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.