CodeIgniter - 如何在视图中打印数组?

时间:2017-09-28 13:09:34

标签: php html arrays codeigniter

抱歉,如果问题接缝很愚蠢,但我在控制器中创建了一个数组,并将其传递给带有$data[]数组的视图。 而且我不能用for循环打印数组。 你能帮我吗?

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

输出:

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

当我在视图中print_r()时会发生这种情况。

我的目标是为自己打印每一个元素!

3 个答案:

答案 0 :(得分:1)

你试过这个吗?

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

使用foreach,您可以单独打印数组元素

请注意,如果您要打印的内容超过第一个索引(我的示例中为0),您应该使用另一个foreach,结果如下:

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

这样你就可以单独打印整个数组了

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

答案 2 :(得分:0)

您可以使用foreach方法打印每个值。

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

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

             endforeach; 
?>