【问题标题】:PHP post data convert to local variablesPHP post数据转换为局部变量
【发布时间】:2017-04-19 00:00:59
【问题描述】:

我有一个 php html 表单提交到另一个页面,这个页面正在提交大量的 post 变量数据。我知道我可以通过定义局部变量 $data1 = $POST["data1"} 将每个发布的变量转换为局部变量。但我想避免这种情况,因为有很多变量。

这里是发布变量的示例转储。

array(59) { ["manufacturer"]=> string(10) "Dell Inc. " ["systype"]=> string(11) "Space-aving" ["model"]=> string(15) "OptiPlex GX520 " ["serial"]=> string(7) "GVHNT1J" ["boardserial"]=> string(17) "..CN6986156K0B61." ["proc_vendor"]=> string(5) "Intel" ["proc_type"]=> string(15) "Central Proceor" ["proc_family"]=> string(9) "Pentium 4" ["proc_cores"]=> string(0) "" ["proc_core_enabled"]=> string(0) "" ["proc_thread_count"]=> string(0) "" ["proc_external_clock"]=> string(7) "800 MHz" ["proc_max_speed"]=> string(8) "4000 MHz" ["proc_speed"]=> string(8) "2800 MHz" ["proc_socket"]=> string(10) "ZIF Socket" ["proc_charateristic"]=> string(0) "" ["proc_cache"]=> string(7) "1024 kB" ["ram"]=> string(4) "3GiB" ["ram_type"]=> string(3) "DDR" ["on_board_device1"]=> string(35) "Intel Graphic Media Accelerator 950" ["on_board_device2"]=> string(42) "Broadcom 5751 NetXtreme Gigabit Controller" ["on_board_device3"]=> string(21) "AC97 Audio Controller" ["on_board_device4"]=> string(4) "None" ["on_board_device5"]=> string(4) "None" ["port_connector1"]=> string(10) "Audio Port" ["port_connector2"]=> string(12) "Network Port" ["port_connector3"]=> string(17) "Parallel Port PS2" ["port_connector4"]=> string(29) "Serial Port 16550A Compatible" ["port_connector5"]=> string(3) "USB" ["port_connector6"]=> string(10) "Video Port" ["port_connector7"]=> string(4) "None" ["port_connector8"]=> string(4) "None" ["storage_controller1"]=> string(58) "Intel Corporation 82801G ICH7 Family IDE Controller rev 01" ["storage_controller2"]=> string(67) "Intel Corporation NM10ICH7 Family SATA Controller [IDE mode] rev 01" ["hard_disk1"]=> string(4) "40GB" ["hard_disk2"]=> string(4) "None" ["hard_disk3"]=> string(4) "None" ["hard_disk4"]=> string(4) "None" ["optic_drive"]=> string(6) "CD-ROM" ["display"]=> string(63) "Intel Corporation 82945GGZ Integrated Graphic Controller rev 02" ["network"]=> string(44) "NetXtreme BCM5751 Gigabit Ethernet PCI Expre" ["wirelessnetwork"]=> string(4) "None" ["multimedia"]=> string(40) "82801G ICH7 Family AC97 Audio Controller" ["camera1"]=> string(4) "None" ["date"]=> string(10) "2017-02-09" ["stime"]=> string(8) "17:35:23" ["grade"]=> string(1) "A" ["uuid"]=> string(36) "44454C4C-5600-1048-804E-C7C04F54314A" ["asset_tag"]=> string(4) "None" ["pwr_on_pwd"]=> string(7) "Enabled" ["key_pwd"]=> string(15) "Not Implemented" ["admin_pwd"]=> string(7) "Enabled" ["front_panel_reset"]=> string(15) "Not Implemented" ["customer"]=> string(9) "Cutomer-A" ["batch"]=> string(14) "Batch-03948383" ["origin"]=> string(8) "Autralia" ["custref"]=> string(6) "Au_cut" ["ourref"]=> string(10) "no_comment" ["comments"]=> string(11) "our_comment" }

我很高兴使用在发布数据中传递的变量名。提前谢谢你。

【问题讨论】:

  • 既然可以引用数组中的值,为什么还要创建变量来引用每个值?

标签: php html variables


【解决方案1】:

这是一个可怕的想法,但如果你必须这样做,你可以使用 extract() 函数:

$array = array(
    'foo' => 'bar',
    'bar' => 'baz'
);

extract($array);

echo $foo; // 'bar'
echo $bar; // 'baz'

更多详情请见http://php.net/extract

【讨论】:

  • 为什么这是一个可怕的想法的例子:stackoverflow.com/questions/829407/…
  • 我试过这个,但对我不起作用。我知道这是一个可怕的想法,但它最适合我的需要。我已经尝试过,但出现 HTML 500 错误。 $array = ($_POST);提取($数组); echo "这是制造商" $manufacturer;
  • 那么你做错了 :-) 检查你的错误日志,看看 PHP 抛出了什么错误导致 Apache 给你一个 500 错误。
【解决方案2】:

您可以使用$$ 循环访问$_POST:

foreach($_POST as $variable => $value)
    $$variable = $value;

那么如果你做一个var_dump($manufacturer):

string(10) "Dell Inc. "

【讨论】:

    【解决方案3】:

    @rickdenhaan 的答案是最好的,但还有另一种方法,这不是一个好主意,当然也不漂亮:

    foreach ($_POST as $key => $value) {
        $$key = $value; // yes, a double $$ - not pretty and a pain to debug!
    }
    

    【讨论】:

      【解决方案4】:

      使用 $_POST 全局变量,获取所有使用键值对提交的值。

      foreach ($_POST as $key => $value){
          $$key=$value;
      }
      

      【讨论】:

      • 这没有回答问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多