访问数组在PHP Multidimention主从到详细信息元素

时间:2016-06-23 05:39:08

标签: php arrays

我有数组Multidimention,我的目的是掌握详细信息。

这个数组看起来像这样:

Array
(
[BSIU2473289] => Array
    (
        [0] => Array
            (
                [ID_BARANG] => 13649
                [THICK] => 0.70
                [SIZE] => 0.70MM X 151.8MM    
                [COIL_NO] => 02NKXTL16064945/48            
                [NET] => 2772
                [GROSS] => 2808
                [CONTRACT_NO] => N512C56LJ02         
                [LOCATION] => 
                [NO_URUT] =>   5785
            )

        [1] => Array
            (
                [ID_BARANG] => 13657
                [THICK] => 0.70
                [SIZE] => 0.70MM X 151.8MM    
                [COIL_NO] => 02NKXTL16065029/36            
                [NET] => 3512
                [GROSS] => 3552
                [CONTRACT_NO] => N512C56LJ02         
                [LOCATION] => 
                [NO_URUT] =>   5784
            )

        )
  )

我想根据这个数组创建一个报告:

报告DUMMY

No : BSIU2473289

CONTAINS :  

[ID_BARANG] => 13649
[THICK] => 0.70
[SIZE] => 0.70MM X 151.8MM    
[COIL_NO] => 02NKXTL16064945/48            
[NET] => 2772
[GROSS] => 2808
[CONTRACT_NO] => N512C56LJ02         
[LOCATION] => 
[NO_URUT] =>   5785
[CONTAINER] => BSIU2473289

等等,等等。

如何进行这样的循环?

<?php foreach ($result as $row): ?>
  <div class="container">
    <div class="header" >
      <!--<img src="D:\xampp\htdocs\develop_tsurumaru\assets\admin\img\logo_tli_web.png">-->
      <!-- <img src="./assets/admin/img/logo_tli_web.jpg"> -->
    </div>

    <div class="document-title"  style='margin-top : 120px;'>
      <hr>
      <h1>Check List<br>Check Sheet for PT. Hanwa Indonesia </h1>
    </div>



    <div class="master" style="width: 40%; display: inline-block">
      <table class="table" style="border: none;">

        <tr style="border:none">
          <td style="height:20px; border: none; text-align: left; width: 30%;">Container No</td>
          <td style="border: none; width:5%;">: </td>
          <td style="border: none; text-align: left; vertical-align: middle;"><?php echo BSIU243289 ?></td>
        </tr>



      </table>
    </div>

    <div style="padding-top : -100px" >
      <table>
        <thead>
          <tr>
            <th rowspan="2">NO</th>
            <th rowspan="2" style="width: 20%">SIZE</th>
            <th rowspan="2" style="width: 18%">CODE OF COIL</th>
            <th rowspan="2">WGHT (NET)</th>
            <th rowspan="2">WGHT (GROSS)</th>
            <th rowspan="2"style="width: 8%">CONTRACT NO</th>
            <th rowspan="2">LOCATION</th>
            <th colspan="3">DENT</th>
            <th rowspan="2">WET</th>
            <th rowspan="2" style="font-size: smaller">NO<br>DMG</th>
            <th rowspan="2">OTH</th>
            <th rowspan="2">NO URUT</th>
          </tr>

          <tr>
            <th style="width: 3%">In</th>
            <th style="width: 3%">Out</th>
            <th style="width: 3%">End</th>
          </tr>

        </thead>

        <tbody>
          <?php
          $i = 1;
          $total_net = 0;
          $total_gross = 0;
          ?>

          <?php foreach ($row as $key => $value) : ?>
            <?php
            echo "<tr>";
            echo "<td>$i</td>";
            echo "<td>". $value['SIZE'] . "</td>";
            echo "<td>". $value['COIL_NO'] . "</td>";
            echo "<td>". $value['NET'] . "</td>";
            echo "<td>". $value['GROSS'] . "</td>";
            echo "<td>". $value['CONTRACT_NO'] . "</td>";
            echo "<td>". $value['LOCATION'] . "</td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td>". $value['NO_URUT'] . "</td>";
            echo "</tr>";

            $total_net += $value['NET'];
            $total_gross = $value['GROSS'];
            $i++;
            ?>
          <?php endforeach;  ?>

          <tr>
            <td colspan="3">Total :</td>
            <td><?= $total_net ?></td>
            <td><?= $total_gross ?></td>
            <td colspan="9"></td>
          </tr>

        </tbody>
      </table>
    </div>


        </tbody>

      </table>
  </div>
</body>
  

3 个答案:

答案 0 :(得分:1)

请检查以下代码。

<?php

$arr = Array
(
'BSIU2473289' => Array
    (
        0 => Array
            (
                'ID_BARANG' => '13649',
                'THICK' => '44',
                'SIZE' => '0.70MM X 151.8MM'   , 
                'COIL_NO' => '02NKXTL16064945/48' ,           
                'NET' => '2772',
                'GROSS' => '2808',
                'CONTRACT_NO' => 'N512C56LJ02'      ,   
                'LOCATION' => '',
                'NO_URUT'=>   '5785'
            ),

        1 => Array
            (
                'ID_BARANG'=> '13657',
                'THICK' => '0.70',
                'SIZE'=> '0.70MM X 151.8MM' ,   
                'COIL_NO' => '02NKXTL16065029/36' ,           
                'NET' => '3512',
                'GROSS' => '3552',
                'CONTRACT_NO' => 'N512C56LJ02'  ,       
                'LOCATION' =>'' ,
                'NO_URUT' =>   '5784'
            )

        ),
  );



$first_key =  key($arr);
foreach ($arr[$first_key] as $key => $value) {

    $chunk = $arr[$first_key][$key];
    foreach ($chunk as $key1 => $value) {
        echo  $arr[$first_key][$key][$key1]."<br>";
    }
}


    ?>

答案 1 :(得分:1)

你错过了foreach声明的结尾

    <?php $a = array(
    "BSIU2473289" => array(
        "0" => array
            (
                "ID_BARANG" => 13649,
                "THICK" => 0.70,
                "SIZE" => "0.70MM X 151.8MM",
                "COIL_NO" => "02NKXTL16064945/48" ,           
                "NET" => 2772,
                "GROSS" => 2808,
                "CONTRACT_NO" => "N512C56LJ02",      
                "LOCATION" => "",
                "NO_URUT" =>   5785,
            ),

        "1" => array
            (
                "ID_BARANG" => 13657,
                "THICK" => 0.70,
                "SIZE" => "0.70MM X 151.8MM",
                "COIL_NO" => "02NKXTL16065029/36",          
                "NET" => 3512,
                "GROSS" => 3552,
                "CONTRACT_NO" => "N512C56LJ02",
                "LOCATION" => "",
                "NO_URUT" =>   5784
            ),
        )
  );

?>
  <div class="container">
    <div class="header" >
      <!--<img src="D:\xampp\htdocs\develop_tsurumaru\assets\admin\img\logo_tli_web.png">-->
      <!-- <img src="./assets/admin/img/logo_tli_web.jpg"> -->
    </div>

    <div class="document-title"  style='margin-top : 120px;'>
      <hr>
      <h1>Check List<br>Check Sheet for PT. Hanwa Indonesia </h1>
    </div>



    <div class="master" style="width: 40%; display: inline-block">
        <table class="table" style="border: none;">
            <tr style="border:none">
              <td style="height:20px; border: none; text-align: left; width: 30%;">Container No</td>
              <td style="border: none; width:5%;">: </td>
              <td style="border: none; text-align: left; vertical-align: middle;"><?php echo BSIU243289 ?></td>
            </tr>
        </table>
    </div>

    <div style="padding-top : -100px" >
      <table>
        <thead>
          <tr>
            <th rowspan="2">NO</th>
            <th rowspan="2" style="width: 20%">SIZE</th>
            <th rowspan="2" style="width: 18%">CODE OF COIL</th>
            <th rowspan="2">WGHT (NET)</th>
            <th rowspan="2">WGHT (GROSS)</th>
            <th rowspan="2"style="width: 8%">CONTRACT NO</th>
            <th rowspan="2">LOCATION</th>
            <th colspan="3">DENT</th>
            <th rowspan="2">WET</th>
            <th rowspan="2" style="font-size: smaller">NO<br>DMG</th>
            <th rowspan="2">OTH</th>
            <th rowspan="2">NO URUT</th>
          </tr>

          <tr>
            <th style="width: 3%">In</th>
            <th style="width: 3%">Out</th>
            <th style="width: 3%">End</th>
          </tr>

        </thead>

        <tbody>
          <?php
          $i = 1;
          $total_net = 0;
          $total_gross = 0;
          ?>

        <?php foreach ($a as $row): ?>
        <?php foreach ($row as $key => $value) : ?>
            <?php
            echo "<tr>";
            echo "<td>$i</td>";
            echo "<td>". $value['SIZE'] . "</td>";
            echo "<td>". $value['COIL_NO'] . "</td>";
            echo "<td>". (int)$value['NET'] . "</td>";
            echo "<td>". (int)$value['GROSS'] . "</td>";
            echo "<td>". $value['CONTRACT_NO'] . "</td>";
            echo "<td>". $value['LOCATION'] . "</td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "<td>". $value['NO_URUT'] . "</td>";
            echo "</tr>";

            $total_net += $value['NET'];
            $total_gross = $value['GROSS'];
            $i++;
            ?>
          <?php endforeach;
          endforeach;  ?>

          <tr>
            <td >Total :</td>
            <td><?php echo $total_net;?></td>
            <td><?php echo $total_gross ?></td>
            <!-- <td colspan="9"></td> -->
          </tr>

        </tbody>
      </table>
    </div>
  </div>

答案 2 :(得分:1)

你的一个foreach循环从未关闭,而且你的一些html标签位于错误的位置 如果您想为每个顶级元素分别使用一个表,例如一个用于BSIU2473289的表和用于BSIU24712345的表,这是如何做到的。 请检查演示中的示例输出。

<?php 
    $result=array ( 'BSIU2473289'=>array ( 0 => array ( 'ID_BARANG' => '13649', 'THICK' => '0.70', 'SIZE' => '0.70MM X 151.8MM', 'COIL_NO' => '02NKXTL16064945/48', 'NET' => '2772', 'GROSS' => '2808', 'CONTRACT_NO' => 'N512C56LJ02', 'LOCATION' => '', 'NO_URUT' => ' 5785', ), 1 => array
    ( 'ID_BARANG' => '13657', 'THICK' => '0.70', 'SIZE' => '0.70MM X 151.8MM', 'COIL_NO' => '02NKXTL16065029/36', 'NET' => '3512', 'GROSS' => '3552', 'CONTRACT_NO' => 'N512C56LJ02', 'LOCATION' => '', 'NO_URUT' => ' 5784', ), ),  'BSIU24712345'=>array ( 0 => array ( 'ID_BARANG' => '13649', 'THICK' => '0.70', 'SIZE' => '0.70MM X 151.8MM', 'COIL_NO' => '02NKXTL16064945/48', 'NET' => '2772', 'GROSS' => '2808', 'CONTRACT_NO' => 'N512C56LJ02', 'LOCATION' => '', 'NO_URUT' => ' 5785', ), 1 => array
    ( 'ID_BARANG' => '13657', 'THICK' => '0.70', 'SIZE' => '0.70MM X 151.8MM', 'COIL_NO' => '02NKXTL16065029/36', 'NET' => '3512', 'GROSS' => '3552', 'CONTRACT_NO' => 'N512C56LJ02', 'LOCATION' => '', 'NO_URUT' => ' 5784', ), ),); 
?>
<div class="container">
    <div class="header" >
      <!--<img src="D:\xampp\htdocs\develop_tsurumaru\assets\admin\img\logo_tli_web.png">-->
      <!-- <img src="./assets/admin/img/logo_tli_web.jpg"> -->
    </div>

    <div class="document-title"  style='margin-top : 120px;'>
      <hr>
      <h1>Check List<br>Check Sheet for PT. Hanwa Indonesia </h1>
    </div>
    <?php 
        foreach($result as $heading=>$row){ 
    ?>
    <div class="master" style="width: 40%; display: inline-block">
      <table class="table" style="border: none;">
        <tr style="border:none">
          <td style="height:20px; border: none; text-align: left; width: 30%;">Container No</td>
          <td style="border: none; width:5%;">: </td>
          <td style="border: none; text-align: left; vertical-align: middle;"><?php echo $heading ?></td>
        </tr>
      </table>
    </div>

            <div style="padding-top : -100px">
              <table>
                <thead>
                  <tr>
                    <th rowspan="2">NO</th>
                    <th rowspan="2" style="width: 20%">SIZE</th>
                    <th rowspan="2" style="width: 18%">CODE OF COIL</th>
                    <th rowspan="2">WGHT (NET)</th>
                    <th rowspan="2">WGHT (GROSS)</th>
                    <th rowspan="2" style="width: 8%">CONTRACT NO</th>
                    <th rowspan="2">LOCATION</th>
                    <th colspan="3">DENT</th>
                    <th rowspan="2">WET</th>
                    <th rowspan="2" style="font-size:smaller">NO<br>DMG</th>
                    <th rowspan="2">OTH</th>
                    <th rowspan="2">NO URUT</th>
                  </tr>
                  <tr>
                    <th style="width: 3%">In</th>
                    <th style="width: 3%">Out</th>
                    <th style="width: 3%">End</th>
                  </tr>
                </thead>

                <tbody>
                <?php
                $i=1 ; 
                $total_net=0 ; 
                $total_gross=0 ; 
                   foreach ($row as $item){ 
                    echo "<tr>"; 
                    echo "<td>$i</td>"; 
                    echo "<td>". $item['SIZE'] . "</td>"; 
                    echo "<td>". $item['COIL_NO'] . "</td>"; 
                    echo "<td>". $item['NET'] . "</td>"; 
                    echo "<td>". $item['GROSS'] . "</td>"; 
                    echo "<td>". $item['CONTRACT_NO'] . "</td>"; 
                    echo "<td>". $item['LOCATION'] . "</td>"; 
                    echo "<td></td>"; 
                    echo "<td></td>"; 
                    echo "<td></td>"; 
                    echo "<td></td>"; 
                    echo "<td></td>"; 
                    echo "<td></td>"; 
                    echo "<td>". $item['NO_URUT'] . "</td>"; 
                    echo "</tr>"; 

                    $total_net += $item['NET']; 
                    $total_gross = $item['GROSS']; 
                    $i++; 
                    } 
                ?>
        <tr>
        <td colspan="3">Total :</td>
        <td><?= $total_net ?></td>
        <td><?= $total_gross ?></td>
        <td colspan="9"></td>
      </tr>
    </tbody>
  </table>
  </div>
  <?php   
    }
  ?>
</div>

示例输出

table {
  color: #333;
  /* Lighten up font color */
  font-family: Helvetica, Arial, sans-serif;
  /* Nicer font */
  width: 640px;
  border-collapse: collapse;
  border-spacing: 0;
}
td,
th {
  border: 1px solid #CCC;
  height: 30px;
}
/* Make cells a bit taller */

th {
  background: #F3F3F3;
  /* Light grey background */
  font-weight: bold;
  /* Make sure they're bold */
}
td {
  background: #FAFAFA;
  /* Lighter grey background */
  text-align: center;
  /* Center our text */
}
<div class="container">
    <div class="header" >
      <!--<img src="D:\xampp\htdocs\develop_tsurumaru\assets\admin\img\logo_tli_web.png">-->
      <!-- <img src="./assets/admin/img/logo_tli_web.jpg"> -->
    </div>

    <div class="document-title"  style='margin-top : 120px;'>
      <hr>
      <h1>Check List<br>Check Sheet for PT. Hanwa Indonesia </h1>
    </div>
        <div class="master" style="width: 40%; display: inline-block">
      <table class="table" style="border: none;">
        <tr style="border:none">
          <td style="height:20px; border: none; text-align: left; width: 30%;">Container No</td>
          <td style="border: none; width:5%;">: </td>
          <td style="border: none; text-align: left; vertical-align: middle;">BSIU2473289</td>
        </tr>
      </table>
    </div>

            <div style="padding-top : -100px">
              <table>
                <thead>
                  <tr>
                    <th rowspan="2">NO</th>
                    <th rowspan="2" style="width: 20%">SIZE</th>
                    <th rowspan="2" style="width: 18%">CODE OF COIL</th>
                    <th rowspan="2">WGHT (NET)</th>
                    <th rowspan="2">WGHT (GROSS)</th>
                    <th rowspan="2" style="width: 8%">CONTRACT NO</th>
                    <th rowspan="2">LOCATION</th>
                    <th colspan="3">DENT</th>
                    <th rowspan="2">WET</th>
                    <th rowspan="2" style="font-size:smaller">NO<br>DMG</th>
                    <th rowspan="2">OTH</th>
                    <th rowspan="2">NO URUT</th>
                  </tr>
                  <tr>
                    <th style="width: 3%">In</th>
                    <th style="width: 3%">Out</th>
                    <th style="width: 3%">End</th>
                  </tr>
                </thead>

                <tbody>
                <tr><td>1</td><td>0.70MM X 151.8MM</td><td>02NKXTL16064945/48</td><td>2772</td><td>2808</td><td>N512C56LJ02</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td> 5785</td></tr><tr><td>2</td><td>0.70MM X 151.8MM</td><td>02NKXTL16065029/36</td><td>3512</td><td>3552</td><td>N512C56LJ02</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td> 5784</td></tr>        <tr>
        <td colspan="3">Total :</td>
        <td>6284</td>
        <td>3552</td>
        <td colspan="9"></td>
      </tr>
    </tbody>
  </table>
   </div>
      <div class="master" style="width: 40%; display: inline-block">
      <table class="table" style="border: none;">
        <tr style="border:none">
          <td style="height:20px; border: none; text-align: left; width: 30%;">Container No</td>
          <td style="border: none; width:5%;">: </td>
          <td style="border: none; text-align: left; vertical-align: middle;">BSIU24712345</td>
        </tr>
      </table>
    </div>

            <div style="padding-top : -100px">
              <table>
                <thead>
                  <tr>
                    <th rowspan="2">NO</th>
                    <th rowspan="2" style="width: 20%">SIZE</th>
                    <th rowspan="2" style="width: 18%">CODE OF COIL</th>
                    <th rowspan="2">WGHT (NET)</th>
                    <th rowspan="2">WGHT (GROSS)</th>
                    <th rowspan="2" style="width: 8%">CONTRACT NO</th>
                    <th rowspan="2">LOCATION</th>
                    <th colspan="3">DENT</th>
                    <th rowspan="2">WET</th>
                    <th rowspan="2" style="font-size:smaller">NO<br>DMG</th>
                    <th rowspan="2">OTH</th>
                    <th rowspan="2">NO URUT</th>
                  </tr>
                  <tr>
                    <th style="width: 3%">In</th>
                    <th style="width: 3%">Out</th>
                    <th style="width: 3%">End</th>
                  </tr>
                </thead>

                <tbody>
                <tr><td>1</td><td>0.70MM X 151.8MM</td><td>02NKXTL16064945/48</td><td>2772</td><td>2808</td><td>N512C56LJ02</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td> 5785</td></tr><tr><td>2</td><td>0.70MM X 151.8MM</td><td>02NKXTL16065029/36</td><td>3512</td><td>3552</td><td>N512C56LJ02</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td> 5784</td></tr>        <tr>
        <td colspan="3">Total :</td>
        <td>6284</td>
        <td>3552</td>
        <td colspan="9"></td>
      </tr>
    </tbody>
  </table>
  </div>
</div>