0

私はPHP配列を持っています:

$curl_options = 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 = array(
    CURLOPT_PORT => 90,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_USERAGENT => Opera/9.02 (Windows NT 5.1; U; en)
);

アレイをデフォルトにリセットするにはどうすればよいですか? に

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

ありがとう。

4

4 に答える 4

2

配列のコピーを作成する必要があります。

$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;
于 2012-05-12T15:27:07.767 に答える
2

これを行う唯一の方法は、配列を元の配列で上書きすることなので、これをもう一度実行してください。

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

PHPはリビジョンデータなどを保存しないため、配列の変更を元に戻すことはできません。

于 2012-05-12T15:27:18.870 に答える
2

「真の」方法は、必要な配列を返す関数 getDefaultOptions を作成することです。

于 2012-05-12T15:29:43.510 に答える
0

2 つの別個の配列を作成します - 1) デフォルト 2) 拡張。

$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);

これで 2 つの配列ができました: そのままの配列$curl_options_defaultと新しい配列 (拡張/置換された要素を含む)$curl_options_new

于 2012-05-12T16:23:51.217 に答える