2015-12-17 282 views
0

獲得陣列的價值我有這種類型的數組,我想獲得CID,totalDistance等的值...通過循環

而且可以有多個CID,totalDistance等,所以我如何使用循環顯示錶格格式的值?

 
Array 
(
    [0] => Array 
     (
      [cid] => 6362165 
      [totalDistance] => 13.727617595403 
      [totalTrips] => 17 
      [avgSpeed] => 4.1792097792906 
      [avgSt] => 1446460214749 
      [avgEt] => 1446460795802 
      [avgScore] => 56 
      [tripIds] => Array 
       (
        [0] => 230529 
        [1] => 230531 
        [2] => 230527 
        [3] => 230528 
        [4] => 230524 
        [5] => 230526 
        [6] => 230520 
        [7] => 230521 
        [8] => 230522 
        [9] => 230523 
        [10] => 230525 
        [11] => 230530 
        [12] => 230532 
        [13] => 230533 
        [14] => 230534 
        [15] => 230535 
        [16] => 230536 
       ) 

     ) 

在這種格式

 

cid totalDistance totalTrips avgspeed avgSt avgEt avgScore tripIds 

val1  val2   val3  val4 val5 val6  val7  val8 

這些值可以是更多,這取決於包含的值的陣列上。

+0

請編輯您的問題標籤以包含該語言。 – DSM

回答

0

我不知道我是否理解正確的問題。但正如我理解的正常foreach循環。

PHP:

$routes = array(
array(
    'cid'    => 1, 
    'totalDistance'  => 13.727617595403, 
    'totalTrips'  => 17, 
    'avgSpeed'   => 4.1792097792906, 
    'avgSt'    => 1446460214749, 
    'avgEt'    => 1446460795802, 
    'avgScore'   => 56, 
    'tripIds'   => array(

    ), 
), 
array(
    'cid'    => 2, 
    'totalDistance'  => 13.727617595403, 
    'totalTrips'  => 17, 
    'avgSpeed'   => 4.1792097792906, 
    'avgSt'    => 1446460214749, 
    'avgEt'    => 1446460795802, 
    'avgScore'   => 56, 
    'tripIds'   => array(

    ), 
)); 

HTML:

<table> 
<thead> 
<tr> 
    <td>cid</td> 
    <td>totalDistance</td> 
    <td>totalTrips</td> 
    <td>avgSpeed</td> 
    <td>avgSt</td> 
</tr> 
</thead> 
<tbody> 
<?php foreach($routes as $route): ?> 
<tr> 
    <td><?php echo $route['cid']; ?></td> 
    <td><?php echo $route['totalDistance']; ?></td> 
    <td><?php echo $route['totalTrips']; ?></td> 
    <td><?php echo $route['avgSpeed']; ?></td> 
    <td><?php echo $route['avgSt']; ?></td> 
</tr> 
<?php endforeach; ?> 
</tbody> 

Mybe你應該考慮使用一個對象,而不是一個數組,但那是你的選擇

+0

您可以跳過他/她問題標題中的「多維」部分。 – cwps