如果为空$ _POST在foreach循环中分配值 [英] if empty $_POST assign value in foreach loop

查看:80
本文介绍了如果为空$ _POST在foreach循环中分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想构建一个创建调查的程序.我不知道如何为未回答的问题赋值.谢谢您的帮助.

$dizi = array();
foreach ( $_POST as $key => $value){
    if(empty($_POST)){
        $_POST="bos"; 
    }
    $dizi[$key] = "'".$value."'"; 
} 

解决方案

$ _ POST是关联数组 因此,您可以通过以下方式访问它:

$bla = $_POST['bla'];

您要尝试将整个数组设置为无效的字符串. 将新值保存到$ dizi数组时,应进行设置.

$dizi = array();
foreach($_POST as $key => $value) {
    $newValue = $value;
    if (empty($value)) {
        $newValue = 'bos';
    }
    $dizi[$key] = $newValue;
    unset($newValue);
}

但这仅检查答案字符串是否为空.因此,这仅在所有问题都是必需的情况下才有效.

Hi want to build a program which creates surveys. I couldn' t figure out how can i assign value for a question which is unanswered. Thank you for your helps.

$dizi = array();
foreach ( $_POST as $key => $value){
    if(empty($_POST)){
        $_POST="bos"; 
    }
    $dizi[$key] = "'".$value."'"; 
} 

解决方案

$_POST is an associative array So you can access it with:

$bla = $_POST['bla'];

What you are trying to do is setting the whole array to a string which doesn't work. You should set the new value when saving it to the $dizi array.

$dizi = array();
foreach($_POST as $key => $value) {
    $newValue = $value;
    if (empty($value)) {
        $newValue = 'bos';
    }
    $dizi[$key] = $newValue;
    unset($newValue);
}

But this only checks if answer string is empty. So this only works if all questions are mandatory.

这篇关于如果为空$ _POST在foreach循环中分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆