2

我正在尝试组织要在电子邮件中发送的数据数组。获取数据没有问题,但我不知道如何组织它。

Foreach: 这里输出用户在后端生成的问题列表

$message = array();

foreach($questions['questions'] as $key => $value){   
       if(is_array($value) && isset($value[ 'questionlist'])){
           foreach($value as $key => $subquestion){ //line 119
               foreach ($subquestion as $key => $value){
                   $message[] = $value['a-question'];                           
               }
           }
       }                     
   }

我试图将数据相互结合,来自 foreach 的$_POST数据和作为检查值的数据。

我这样做的逻辑是因为一个来自数据库,一个只是表单数据(与通过后端生成的数据库中的数据不同,它不需要保存到来自前端的数据库)说也许有更好的方法,但我几乎明白了我只是不确定如何加入数据,所以看起来像

    <li>MYARRAYDATA - MYFORMDATA</li>
    <li>MYARRAYDATA - MYFORMDATA</li>
    <li>MYARRAYDATA - MYFORMDATA</li>



    //The form input data '0', '1' values
    $checks = $_POST['personalization_result'];

    //Putting that data into array_values
    $checkValues = array_values($checks);    

    //Then passing the array_values into 'implode' and organizing it with a list (<li>)
    $checkString = '<li>'.implode('</li><li>',$checkValues).'</li>';

    //Then testing with var_dump outputs a nice list of '0','1' values
    var_dump ($checkString);

尝试相同的方法但尝试合并foreach数组和检查值不起作用,这是一个示例。

    //Similar to $checkValues I pass the data from the foreach into "array_values"
    var_dumping this works fine.
    $arrayValues = array_values($message);

    //This is obvious it's the same as above it "implodes" the data nicely into a list(<li>)       
    $arrayString = '<li>'.implode('</li><li>',$arrayValues).'</li>';

    //This var dumps the "$arrayString" nicely
    var_dump ($arrayString)

实际问题又来了,我如何连接每条数据?

我的尝试: 这是我“合并”数据的尝试。

    // This does not work well (maybe by cleaning up it can work) but it outputs 2 separate lists
    var_dump ($arrayString.'_'.$checkString);

    //I tried to run it inside one implode variable this is invalid arguments
    $checkString = '<li>'.implode('</li><li>',$arrayValues.'_'.$checkValues).'</li>';

    //Modified one implode variable this outputs see below 
    $checkString = '<li>'.implode('</li>'.$arrayValues.'<li>',$checkValues).'</li>';

    <li>Array
    1</li>
    <li>Array
    0</li>
    <li>Array
    1</li>
    <li>Array
    0</li>

var_dump 结果: 这是每个数组的 var_dump 结果,我想将它们组合成一个列表

$_POST 数组

// Var dump of form $_POST DATA
var_dump ($checkString);

//Result
    1      //This is generated through the $_POST method not on database
    0      //This is generated through the $_POST method not on database
    1      //This is generated through the $_POST method not on database
    0      //This is generated through the $_POST method not on database

数据库数组

// Var dump of datbase generated from backend
var_dump ($arrayString);

//Result 
    I am 1         //This is generated in the backend and is stored on a database
    Hi I am 2      //This is generated in the backend and is stored on a database
    civil is 3     //This is generated in the backend and is stored on a database
    THIS IS FOURTA //This is generated in the backend and is stored on a database

目标

     I am 1         - 1 //This is checked
     Hi I am 2      - 0 //This is NOT checked
     civil is 3     - 1 //This is checked
     THIS IS FOURTA - 0 //This is NOT checked

东西

答案: 感谢@xdim222

起初我不明白,因为增量,但现在我明白了,最初它会起作用,但我的变量在 foreach 语句下,这导致它不返回数组。

至少在我看来就是这样,因为当我在 foreach 上方添加变量时它起作用了。

我修改了答案以适合我的代码。

    //$messages = array('test1', 'test2', 'test3', 'test4', 'test5');
    //Instead of using this array I used the array generated in my foreach above.

    // Instead of this $checks = array(1,0,1,0); I take the $_POST value which generates an array, you can see above.
    $checkValues = array_values($checks);

    $checkString = implode($checkValues);

    $i=0;
    foreach($messages as $msg) {
        echo $msg . ' - ' . ( isset($checkString[$i])? $checkString[$i] : 0 ) . '<br>';
    $i++;
    }

再次感谢@xdim222的耐心,阅读我的长问题,最重要的是帮助我学习,通过提出这个问题并找到解决方案,这些东西真的很有效,在我看来是最好的学习方式(通过提问)。:)

4

3 回答 3

2

为了更容易,我直接分配 $messages 和 $checks,我已经尝试过这段代码并且它可以工作。您的数组中可能有不同的元素,但我认为您可以从下面的代码中弄清楚:

  $messages = array('test1', 'test2', 'test3', 'test4', 'test5');

  $checks = array(1,0,1,0);

  $i=0;
  foreach($messages as $msg) {
    echo $msg . ' - ' . ( isset($checks[$i])? $checks[$i] : 0 ) . '<br>';
    $i++;
  }

PS:我在之前的回答中犯了一个错误,在回显之前增加了 $i,数组元素应该从 0 开始。

于 2013-08-11T08:20:02.753 回答
1

在等待您对我上面评论的回复时,我正在尝试在这里编写一些代码..

我假设您想要显示从数据库中提取的问题,并且应该根据用户在表单中选择的内容来显示。因此,您可以使用以下代码:

foreach($questions['questions'] as $key => $value){   
   if(is_array($value) && isset($value[ 'questionlist'])){
       foreach($value as $key => $subquestion){ //line 119
           foreach ($subquestion as $key => $value){
               $message[$key] = $value['a-question'];                           
           }
       }
   }                     

}

我在上面的代码中在 $message 中添加了 $key,假设 $key 是问题的索引,并且该索引将与用户在表单中选择的内容相匹配。然后我们可以列出用户选择的所有问题:

foreach($checks as $check) 
  echo '<li>'.$check . ' - ' . $message[$check].'</li>';

这是你想要的吗?

于 2013-08-11T04:49:17.347 回答
1

根据您的更新:

$i=0; 
foreach($message as $msg) {
  $i++;
  echo '<li>'. $msg . ' - ' . ( isset($checks[$i])? $checks[$i] : 0 ) . '</li>';
}

也许这就是你想要的。

正如我所说,$message 和 $checks 之间应该有关系,否则上面的代码看起来有点奇怪,因为我们怎么知道用户选择了一个问题?也许您应该展示如何在 HTML 中获得 $_POST['personalization_result'] 。

于 2013-08-11T05:06:12.963 回答