繁体   English   中英

用常用名称替换常量名称的PHP数组的键?

[英]Replace key of a PHP array by the value of a constant with common name?

标题可能听起来令人困惑,所以这里是清楚解释的代码。

我在PHP脚本中编写了以下代码

    $options=array(
        CURLOPT_URL => 'http://site2sms.com/userregistration_next.asp',
        CURLOPT_REFERER => 'http://site2sms.com/UserRegistration_Next.asp',
        CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31',
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => http_build_query($post_fields)
    );

但是,当使用var_dump函数转储数组时,这就是我得到的

array (size=5)
  10002 => string 'http://site2sms.com/userregistration_next.asp' (length=45)
  10016 => string 'http://site2sms.com/UserRegistration_Next.asp' (length=45)
  10018 => string 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' (length=108)
  47 => int 1
  10015 => string 'action=UserCreate&txtFullName=fdsf&genderCombo=Male&birth_day=2&birth_month=12&birth_year=2013&txtEmail=fdsf%40dssad&ProfessCombo=1&StateCombo=Delhi&txtMobileNum=4234&cityCombo=223&Submit=Register' (length=196)

显然,常量CURLOPT_URL的值在其转储中被替换为10002 所以,我用这个替换了原来的数组

    $options=array(
        'CURLOPT_URL' => 'http://site2sms.com/userregistration_next.asp',
        'CURLOPT_REFERER' => 'http://site2sms.com/UserRegistration_Next.asp',
        'CURLOPT_USERAGENT' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31',
        'CURLOPT_POST' => TRUE,
        'CURLOPT_POSTFIELDS' => http_build_query($post_fields)
    );

获得此转储价值

array (size=5)
  'CURLOPT_URL' => string 'http://site2sms.com/userregistration_next.asp' (length=45)
  'CURLOPT_REFERER' => string 'http://site2sms.com/UserRegistration_Next.asp' (length=45)


  'CURLOPT_USERAGENT' => string 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' (length=108)
  'CURLOPT_POST' => boolean true
  'CURLOPT_POSTFIELDS' => string 'action=UserCreate&txtFullName=fdsf&genderCombo=Male&birth_day=2&birth_month=12&birth_year=2013&txtEmail=fdsf%40dssad&ProfessCombo=1&StateCombo=Delhi&txtMobileNum=4234&cityCombo=485&Submit=Register' (length=196)

现在,我收到一个错误Warning: curl_setopt_array(): Array keys must be CURLOPT constants or equivalent integer values 那么,我如何调整数组,以便转储数组显示常量名称而不是值,当与curl_setopt_array使用时,它应该可以很好地工作。 我正在寻找一些可以比curl_setopt_array函数更早使用的函数,以便它可以对数组进行必要的更改。 如果通过PHP内置函数无法实现,请建议我如何手动创建此功能。

甚至还有一种紧凑的方式:

curl_setopt_array($ch,
    array_combine(
        array_map("constant", array_keys($options)),
        array_values($options)
    )
);

解释一下:

  • constant()将一个字符串常量名称转换为其值

  • array_keys()只是从$ options数组中提取键

  • array_map()对每个键应用constant ,返回其值,但保持现在的整数键列表的顺序

  • array_values()返回$ options值的索引列表

  • finally array_combine()使用它们的值重新合并两个仍然有序的数字键

由于你主要需要这个来控制卷曲 ,我还想在这里提出一些替代方案。 不是针对整体问题,而是针对手头的具体任务。

我个人使用一个小型的混合/流畅的包装 curl.php这些事情。 它可能通常比阵列选项方法更短:

$result =
    curl()
       ->URL('http://site2sms.com/userregistration_next.asp')
       ->REFERER('http://site2sms.com/UserRegistration_Next.asp')
       ->USERAGENT('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/')
       ->POST(true)
       ->POSTFIELDS(http_build_query($post_fields))
       ->exec();

这减少了所有curl_函数和CURL_常量前缀。
(但它仍然有->setopt_array()可用,顺便说->setopt_array() 。)

CURLOPT_URL (不带引号)只是整数10002另一个名称 - 当你使用它时,它被翻译。 这是一个单向的过程!

如果要使用字符串表示,则需要使用临时表:

$dumpableoptions=array(
    'CURLOPT_URL' => 'http://site2sms.com/userregistration_next.asp',
    ...
);

$translations=array(
    'CURLOPT_URL' => CURLOPT_URL,
    ...
);

然后

$options=array();
foreach ($dumpableoptions as $key=>$value)
  $options[$translations[$key]]=$value;

或者你必须利用恐怖的恐怖: eval() 我不愿意为此提供代码。

编辑

在阅读@ mario的评论之后,我发现,自从版本2开始在PHP中开发之后,我还有很多需要学习的东西! 我根本就不知道constant()函数。 这变化很大:

  • 忘记临时表,它在cURL扩展名内
  • 将以后的代码更改为

$options=array();
foreach ($dumpableoptions as $key=>$value)
  $options[constant($key)]=$value;

这是一个示例转储功能。 传递数组和它将用于地图的常量的前缀。

<?php

function const_keyed_array_dump($arr, $const_prefix) {
        static $map = array();

        // Primative caching
        if (!$map) {
                foreach (get_defined_constants() as $name=>$val) {
                        if (strpos($name, $const_prefix) === 0) {
                                $map[$val] = $name;
                        }
                }
        }

        $output = array();

        foreach ($arr as $key=>$val) {
                $output[$map[$key]] = $val;
        }

        return $output;
}

var_dump(
        const_keyed_array_dump(
                array(
                        CURLOPT_DNS_USE_GLOBAL_CACHE => 'foo',
                        CURLFTPSSL_TRY => 'bar',
                ),
                'CURL'
        )
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM