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"]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?