doufei3561 2013-08-11 03:37
浏览 110
已采纳

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

I am trying to organize an array of data to be sent in an email. I have no problem getting the data, but I am not sure how to organize it.

Foreach: Here outputs a list of questions generated by the user in the backend

$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'];                           
               }
           }
       }                     
   }

I am trying to conjoin the data with each other, the data from the foreach and the $_POST data which is check values.

My logic for doing it this way is because one comes from the database, one is just form data (that does not need to be saved to the database it comes from the front end unlike the data from the database that is generated via backend) That said there perhaps is a better way, but I pretty much got this I am just not sure how to join the data so it looks like

    <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);

Trying the same method but trying to conjoin the foreach array and the check values does not work, here is an example.

    //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)

Again the actual question is here, how do I conjoin each piece of data?

My Attempts: Here are my attempts for "conjoining" the data.

    // 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 results: Here is the var_dump result of each array, I want to combine these into one list

$_POST array

// 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 array

// 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

The Goal

     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

The Answer: Thanks to @xdim222

I didn't understand it at first, because of the increment, but now I understand it all, initially it would have worked but my variables were under the foreach statement and that was causing it not to return the array.

At least in my opinion thats what it was, because when I added the variable above the foreach it worked.

I modified the answer to suit my code.

    //$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++;
    }

Again thanks to @xdim222 for being patient, reading my long question, and most importantly helping me learn, by asking this question and finding a solution this stuff really sticks and is in my opinion the best way to learn (by asking). :)

  • 写回答

3条回答 默认 最新

  • ds34222222 2013-08-11 08:20
    关注

    To make it easier, I assign $messages and $checks directly, I have tried this code and it works. You might have different elements of your arrays, but I think you can figure it out from my code below:

      $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 made a mistake in my previous answer by incrementing $i before echoing things out, array element should start by 0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案