使用“array_values”和“foreach”数据?

时间:2013-08-11 03:37:27

标签: php arrays foreach

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

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

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

stuff

答案: 感谢@ 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 有耐心,阅读我的长问题,最重要的是帮助我学习,通过提出这个问题并找到解决方案,这些东西真的很棒,在我看来是最好的学习的方式(通过询问)。 :)

3 个答案:

答案 0 :(得分: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开始。

答案 1 :(得分: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>';

这是你想要的吗?

答案 2 :(得分:1)

根据您的更新:

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

也许这就是你想要的。

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

相关问题