duanjue9296 2017-04-18 11:47
浏览 56
已采纳

如何使用PHP中的foreach循环将特定值存储到$ _POST数组内的另一个数组中

I am having following values in an array as I did print_r($_POST);

Array
 (
   [prod_category] => 2
   [prod_for] => 2
   [prod_brand] => 1
   [prod_name] => this is a product
   [prod_price] => 100
   [prod_discount] => 102
   [prod_sizes] => s,m,l,xl,xxl,xxxl,41,42,43,44,45
   [prod_colors] => orange,white,red,blue
   [prod_description] => this is a demo product descrption
   [prod_stock] => 100
 )

What I want to do is store the value [prod_sizes] => s,m,l,xl,xxl,xxxl,41,42,43,44,45 which is in the array into a new array variable using foreach loop, so it looks like

Array
 (
   [0] => s
   [1] => m
   [2] => l
   [3] => xl
   [4] => xxl
   [5] => xxxl
   [6] => 41
   [7] => 42
   [8] => 43
   [9] => 44
   [9] => 45
 )

How to achieve it I am using the following code:

$sizes = $temp = array();
foreach ($_POST as $key => $_POST["prod_sizes"])
    {
        $temp = explode(',', $_POST["prod_sizes"]);
        $sizes[] =  $temp[0];
    }
print_r($sizes);

But i am getting output something like this which was not expected.

Array
(
  [0] => 2
  [1] => 2
  [2] => 1
  [3] => this is a product
  [4] => 100
  [5] => 102
  [6] => s
  [7] => orange
  [8] => this is a demo product descrption
  [9] => 100
)

In above values it's only showing the 1st values encountered in each variable.

Anyone can help me with this logic,

Thanks in advance

  • 写回答

1条回答 默认 最新

  • douweicheng5532 2017-04-18 11:49
    关注

    You do not need any loop here.

    Just do:

    $sizes = explode(',', $_POST["prod_sizes"]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播