douzi115522 2013-08-07 15:05
浏览 256
已采纳

将foreach中的数组传递给foreach外的变量?

Im taking data from a form and e-mailing it using wp_mail, since there is multiple data (all check box data) there obviously needs to be a foreach.

I cannot wrap my head around getting the data inside the foreach and using it outside the statement.

 //My form data
 $checks = $_POST['personalization_result'];

 //Pass the foreach array into this variable and use this to mail
 $checkString = '';

    //For each checkbox data
    foreach ($checks as $k => $v) {
     var_dump ($v);
    }

 //Email the data
 $sent = wp_mail($to, $subject, $checkString, $headers); //I have set up the other variables but its not necessary to add here just focused on $checkString

Snippet of the form

<form action="<?php the_permalink(); ?>" method="post"> 

<?php echo $checkString; ?>// Trying to see what the array is doing

<input type="hidden" name="submitted" value="1">

<p><input type="submit"></p>

<li class="option table selected">
    <input type="hidden" value="0" name="personalization_result[memory_0]">
    <input type="checkbox" value="1" name="personalization_result[memory_0]" id="personalization_result_memory_0" checked="checked">                                        
</li> 


<li class="option table selected">
    <input type="hidden" value="0" name="personalization_result[memory_1]">
    <input type="checkbox" value="1" name="personalization_result[memory_1]" id="personalization_result_memory_1" checked="checked">                                    
</li>  

Im sorry if this is a bit noobish, but this is how I learn by asking.

  • 写回答

1条回答 默认 最新

  • dongnuochen9449 2013-08-07 15:12
    关注

    Since wp_mail's third parameter message is a string, I think you can avoid the foreach altogether and use array_values to get values as an array and join them with some glue instead of looping through the $checks array. Please try:

    $checkValues = array_values($checks);
    $checkString = implode(',', $checkValues); // This generates comma separated list of values.
    

    You could use any glue (comma in this example) you like in the implode function call based on how you want to display the .$checkValues

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法