0

sorry if the questions seams stupid but I have created an array in my controller and I pass it to the view with the $data[] array. And I don't get it right to print the array with a for loop. Can you please help me?

$measurearray = array(
    'nr' => $answerNr,
    'responsible' => $responsible,
    'creationdate' => $InvDate2,
    'activity' => $reason
);
$arr[$i] = $measurearray;
$data["failedMeasures_nr"] = $arr;

Output :

Array ( 
  [50] => Array ( 
    [0] => Array ( 
      [nr] => 5d 
      [responsible] => werner 
      [creationdate] => asdfgdf 
      [activity] => appointed. 
    ) 
  ) 
  [73] => Array ( 
    [0] => Array ( 
      [nr] => 9g 
      [responsible] => 42887 
      [creationdate] => Zuzana 
      [activity] => r the training. 
    ) 
  ) 
) 

This is what happens when I print_r() it in the view.

My target is to print every single element for itself!

3 Answers 3

1

Have you tried this?

foreach ( $failedMeasures_nr[0] as $key => $val ) {
   echo $key . ' is ' . $val; 
}

With a foreach you can print separately the array elements

Please note that if you want to print more than the first index ( the 0 in my example ) you should use another foreach, resulting in something like this:

foreach ( $failedMeasures_nr as $new_arr ) {
    foreach ( $new_arr as $key => $val ) {
       echo $key . ' is ' . $val; 
    }
}

this way you will print the whole array separately

Sign up to request clarification or add additional context in comments.

Comments

0

You can use foreach method to print each value.

<?php 
             foreach ($failedMeasures_nr as $print):

                            echo $print['nr'];
                            echo $print['responsible'];
                            echo $print['creationdate']; 
                            echo $print['activity'];

             endforeach; 
?>

Comments

0
// controller page try this,
$data[$i]["failedMeasures_nr"] =array('nr' => $answerNr,
                                  'responsible' => $responsible,
                                  'creationdate' => $InvDate2,
                                  'activity' => $reason);
$this->load->view('view_page',$data);

In View Page try this code,

for($i =0 ; $i <count($data);$i++){
   foreach ($data[$i] as $key => $value) {
       echo "$value";
       echo "<br>";
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.