2016-08-15 4 views
0

どのようにforeachループの配列を数えますか?また、これはこのコードを書いている最善の方法ですか、それとももっとたくさんのcatchallがありますか?私は3次元配列(3つのレベル)を持っています。ここで多次元配列をforeachループ(php)に入れる方法を数えますか?

がしますprint_r

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コードです:

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

どのように私はこのforループのために、この配列を数えることができますか?

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

をカウントされるだろう、それはオープンと文の間には密接にしようとして続けました。意味をなさない:) –

0

この文を実行して後で印刷することができます。 が、私はこの

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

$値の要素数[class_answer]は$私はそれについて忘れてしまった

print $count 
+0

foreachループに囲まれている私は推定しています –

関連する問題