Using "array_values" with "foreach" data?

Mike

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). :)

xdim222

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related