doujia7517 2014-06-25 16:47
浏览 20
已采纳

PHP访问数组值的正确方法是什么

This seems like a greenhorn question but I am having a hard time here.

I have an array of post values from a form posted via jquery, the $_POST["extra"] array is formatted via .serialize().

string(1) "0"
["tax"]=>string(1) "0"
["taxRate"]=>string(1) "0"
["itemCount"]=>string(1) "3"
["item_name_1"]=>string(17) "Detox Diet 1234®"
["item_quantity_1"]=>string(1) "2"
["item_price_1"]=>string(5) "32.95"
["item_options_1"]=>string(56) "thumb: /cb2014-II/img/products/DetoxDiet1234.jpg, pid: 3"
["item_name_2"]=>string(12) "Fiber 1234®"
["item_quantity_2"]=>string(1) "1"
["item_price_2"]=>string(2) "55"
["item_options_2"]=>string(52) "thumb: /cb2014-II/img/products/Fiber1234.jpg, pid: 4"
["item_name_3"]=>string(10) "eAc 1234®"
["item_quantity_3"]=>string(1) "2"
["item_price_3"]=>string(5) "42.95"
["item_options_3"]=>string(40) "pid: 27, thumb: img/products/eAC1234.jpg"
["extra"]=>string(465) "x_first_name=value&x_last_name=value&phone=xxx-xxx-xxxx&fax=&email=first.last%40domain.com&company=company&ccnumber=xxxxxxxxxxx&expMo=1&expYr=2016&cvv=&address1=address one&address2=&city=city&state=UT&zip=xxxxx&country=USA&addressee_firstName=&addressee_lastName=&shipping_address1=&shipping_address2=&shipping_city=&shipping_state=&shipping_zip=&shipping_country=&checkoutConfirm=1&x_amount=xx.xx&num_units=3&x_test_request=yes"
["testItem"]=>string(9) "test Item"
["check_out"]=>string(3) "Yes"
}

To access the posted values inside of "extra" is the correct syntax $newVar = $_POST['extra']['num_units']?

Because it seems like the values being printed to my screen are the first character of the first 'extra' field name, I am not using a loop to fetch the values just trying to set locally after sanitizing.

MY SOLUTION: parse_str($extra,$extraFields); No I can access the values like $extraFields['x_first_name']

  • 写回答

2条回答 默认 最新

  • dst67283 2014-06-25 16:53
    关注

    See this http://www.php.net//manual/en/function.parse-str.php

    $extras = array();
    parse_str($_POST['extra'], $extras);
    //now $extras holds all the info in a readable format
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?