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.

Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
Serge
  • 35
  • 1
  • 5
  • 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 :) – siaooo May 12 '12 at 19:01

4 Answers4

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;
John Conde
  • 217,595
  • 99
  • 455
  • 496
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.

Jeroen
  • 13,056
  • 4
  • 42
  • 63
2

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

Adelf
  • 706
  • 4
  • 12
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

Roman
  • 3,799
  • 4
  • 30
  • 41