如何将PHP数组重置为其默认键和值
我有PHP数组:如何将PHP数组重置为其默认键和值,php,arrays,defaults,Php,Arrays,Defaults,我有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 => 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
);
谢谢。您需要复制阵列:
$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;
您需要制作阵列的副本:
$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;
执行此操作的唯一方法是使用原始阵列覆盖阵列,因此只需再次运行此操作:
$curl_options = array(
CURLOPT_PORT => 80,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30);
PHP不存储任何修订数据或类似数据,因此您无法反转数组更改。唯一的方法是使用原始数组覆盖数组,因此只需再次运行此操作:
$curl_options = array(
CURLOPT_PORT => 80,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30);
PHP不存储任何修订数据或类似的内容,因此不能反转数组更改。正确的方法是创建一个函数getDefaultOptions,返回所需数组。正确的方法是创建一个函数getDefaultOptions,返回所需数组。创建两个单独的数组-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);
现在您有了两个数组:未接触的$curl\u options\u default
和new(带有扩展/替换的元素)$curl\u options\u new
创建两个单独的数组-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);
现在您有两个数组:未触及的
$curl\u options\u default
和new(带有扩展/替换的元素)$curl\u options\u new
告诉我们您在这里到底想做什么,也许我们可以为您提供适合您的解决方案。请注意,所有这些答案都有效,而且都非常简单。所以,让我总结一下,你的问题不可能那么简单,你需要一些其他的解决办法。但如果你的问题真的那么简单,请选择一个作为答案:)告诉我们你到底想做什么,也许我们可以为你提供适合你的解决方案。请注意,所有这些答案都有效,而且都非常简单。所以,让我总结一下,你的问题不可能那么简单,你需要一些其他的解决办法。但如果你的问题真的那么简单,请选择一个作为答案:)