我正在尝试组织要在电子邮件中发送的数据数组。获取数据没有问题,但我不知道如何组织它。
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的耐心,阅读我的长问题,最重要的是帮助我学习,通过提出这个问题并找到解决方案,这些东西真的很有效,在我看来是最好的学习方式(通过提问)。:)