2016-08-15 4 views
0

배열에서 foreach 루프를 계산하려면 어떻게합니까? 또한,이 코드를 작성하는 가장 좋은 방법입니까, 아니면 더 많은 catchall입니까? 3 차원 배열 (3 단계)이 있습니다.다차원 배열을 foreach 루프 (PHP)에 포함시키는 방법은 무엇입니까?

$newArray = []; 
foreach($requirements as $key => $value){ 
    $newArray[] = $value['audit_inspectionID']; 
    $newArray[] = $value['requirement']; 
    $newArray[] = $value['class_answer']; 
    $newArray[] = $value['repeat_answer']; 
    $newArray[] = $value['class_answer']; 
    $newArray[] = $value['actionID']; 
    $newArray[] = $value['action_link']; 

    print "<div id='inspection_view" . $value['audit_inspectionID'] . "' style='display:inline'> 
     <table id='actions_table' class='table table-bordered table-condensed table-striped bg-info'> 
      <thead> 
       <th align='center'>" . value['requirement'] . " </th> 
       ". if ($corporate_admin == 'true') { ." 
       <a id='" . $value['audit_inspectionID'] . "' class='btn btn-danger pull-right remove1' href=' + '#' + '>Remove</a><a id='edit" . $value['audit_inspectionID'] ."' class='btn btn-warning pull-right edit1' href=#>Edit</a></th> 
       ". } ." 
       </thead><tbody> 
       <tr> 
        <td> 

가 어떻게 루프 배열이 본 셀 수 : 여기

여기
Array 
(
[0] => Array 
    (
     [0] => 17 
     [audit_inspectionID] => 17 
     [1] => 2016-08-15 
     [created] => 2016-08-15 
     [2] => 2016-08-15 09:52:28 
     [modified] => 2016-08-15 09:52:28 
     [class_answer] => Array 
      (
       [0] => Needs Improvement  
       [1] => Need To Correct  
       [2] => Needs Immediate Action  
      ) 
    ) 

) 

PHP 코드가 인 print_r

의 일례이다?

    ". for (x = 0; x< count($value[class_answer]) ; x++){ 
         $value[class_answer] 
        } ." 
        <br> 
        ". $value[repeat_answer] ." 
        <br> 
        ". if ($value[actionID] != 0) { 
        $value[action_link] 
        } ." 
        </td> 
       </tr> 
      </tbody> 
     </table> 
     </div>";/**/ 
} 

답변

0

당신은 print 문 내부 for 또는 if 같은 다른 문을 넣을 수 없습니다. print 문을 종료 한 다음 새 문을 사용하여 원하는 작업을 수행하십시오.

print "<div id='inspection_view" . $value['audit_inspectionID'] . "' style='display:inline'> 
    <table id='actions_table' class='table table-bordered table-condensed table-striped bg-info'> 
      <th align='center'>" . value['requirement'] . " </th> 
      <tr> 
       <td>"; 
foreach ($value['class_answer'] as $ans) { 
    print "$ans "; 
} 
print "<br> 
     ".$value[repeat_answer]." 
     <br>"; 
if ($value['actionID'] != 0) { 
    print $value['action_link']; 
} 
print "</td> 
      </tr> 
     </tbody> 
    </table> 
    </div>"; 
+0

계산되어 $ 값 [class_answer]의 요소의

print "<div id='inspection_view" . $value['audit_inspectionID'] . "' style='display:inline'> <table id='actions_table' class='table table-bordered table-condensed table-striped bg-info'> <th align='center'>" . value['requirement'] . " </th> <tr> <td>"; $count=0; for (x = 0; x< count($value[class_answer]) ; x++){ $count++; } print "<br>". $value[repeat_answer] ."<br>"; if ($value[actionID] != 0) { print $value[action_link]; } print "</td> </tr> </tbody> </table> </div>"; 

수를 할 것입니다. 말이된다. :) –

0

문을 실행하고 나중에 인쇄 할 수 있습니다. 나는 $ 나는 그것에 대해 잊고는 열고 닫는 문 사이에 만들려고 유지

print $count 
+0

foreach 루프로 둘러싸인 나는 추정하고있다. –

관련 문제