2017-07-10 61 views
0
Array 
(
[25p] => Array 
    (
     [0] => Array 
      (
       [id] => 353 
       [label] => C81 
       [casting] => 21 Mar 2017 
       [data] => Array 
        (
         [0] => Array 
          (
           [x] => 1 
           [y] => 6.220000 
          ) 

         [1] => Array 
          (
           [x] => 2 
           [y] => 10.220000 
          ) 

         [2] => Array 
          (
           [x] => 7 
           [y] => 21.440000 
          ) 

         [3] => Array 
          (
           [x] => 28 
           [y] => 30.780000 
          ) 

        ) 

      ) 

     [1] => Array 
      (
       [id] => 354 
       [label] => C82 
       [casting] => 21 Mar 2017 
       [data] => Array 
        (
         [0] => Array 
          (
           [x] => 1 
           [y] => 8.000000 
          ) 

         [1] => Array 
          (
           [x] => 3 
           [y] => 20.890000 
          ) 

         [2] => Array 
          (
           [x] => 6 
           [y] => 30.670000 
          ) 

         [3] => Array 
          (
           [x] => 7 
           [y] => 32.555000 
          ) 

         [4] => Array 
          (
           [x] => 28 
           [y] => 51.110000 
          ) 

        ) 

      ) 

     [2] => Array 
      (
       [id] => 355 
       [label] => C83 
       [casting] => 21 Mar 2017 
       [data] => Array 
        (
         [0] => Array 
          (
           [x] => 1 
           [y] => 7.110000 
          ) 

         [1] => Array 
          (
           [x] => 3 
           [y] => 19.780000 
          ) 

         [2] => Array 
          (
           [x] => 6 
           [y] => 23.560000 
          ) 

         [3] => Array 
          (
           [x] => 7 
           [y] => 32.225000 
          ) 

         [4] => Array 
          (
           [x] => 15 
           [y] => 38.890000 
          ) 

        ) 

      ) 

     [age] => Array 
      (
       [0] => 1 
       [1] => 2 
       [2] => 3 
       [3] => 6 
       [4] => 7 
       [5] => 15 
       [6] => 28 
      ) 

    ) 

) 

我想創建一個基於[age]值的表作爲標題。我設法創建了表格,但問題是,我的數據未正確放置。 [age]基於[data][x]值的唯一平坦列表。從陣列中的數據,C83不應該有數據爲2D,因爲下面沒有[data][x] = 2從不同長度和值的數組創建表php

+---------------+----------+-----------+-----------+-----------+-----------+-----+-----+ 
| Monitor Label | 1D | 2D  | 3D  | 6D  | 7D  | 15D | 28D | 
+---------------+----------+-----------+-----------+-----------+-----------+-----+-----+ 
| C81   | 6.220000 | 10.220000 | 21.440000 | 30.780000 |   |  
|  | 
| C82   | 8.000000 | 20.890000 | 30.670000 | 32.555000 | 51.110000 |  
|  | 
| C83   | 7.110000 | 19.780000 | 23.560000 | 32.225000 | 38.890000 |  
|  | 
+---------------+----------+-----------+-----------+-----------+-----------+-----+-----+ 

是我的代碼生成以上

//test creating HTML 
foreach($graph as $grade => $data) 
{ 
    echo '<strong>'.strtoupper($grade).'</strong></br>'; 
    //start the table for each grade 
    echo '<table border=1>'; 
    echo '<thead>'; 
    echo '<tr>'; 
    echo '<th>Monitor Label</th>'; 

    for($i=0;$i < count($data['age']);$i++) 
    { 
     echo '<th>'.$data['age'][$i].'D</th>'; 
    } 
    echo '</tr>'; 
    echo '</thead>'; 
    echo '<tbody>'; 

    for ($i=0;$i < count($data)-1; $i++) 
    { 
    echo '<tr>'; 
    echo '<td>'.$data[$i]['label'].'</td>'; 
    foreach($data[$i]['data'] as $x) 
    { 
     echo '<td>'.$x['y'].'</td>'; 
    } 
    echo '</tr>'; 
    } 

    echo '</tbody>'; 
    echo '</table>'; 
} 

表的一些[data][y]應該是失蹤,我想更換它帶有 - (短劃線)或#NA,但它只是填充表格列。

如果有人有興趣的數據,這裏是實際的數據串行

a:4:{s:3:"30n";a:2:{i:0;a:4:{s:2:"id";i:423;s:5:"label";s:3:"D43";s:7:"casting";s:11:"05 Apr 2017";s:4:"data";a:5:{i:0;a:2:{s:1:"x";i:1;s:1:"y";s:8:"7.330000";}i:1;a:2:{s:1:"x";i:2;s:1:"y";s:9:"13.560000";}i:2;a:2:{s:1:"x";i:3;s:1:"y";s:9:"16.890000";}i:3;a:2:{s:1:"x";i:7;s:1:"y";s:9:"38.000000";}i:4;a:2:{s:1:"x";i:27;s:1:"y";s:9:"32.670000";}}}s:3:"age";a:5:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:7;i:4;i:27;}}s:3:"30p";a:5:{i:0;a:4:{s:2:"id";i:428;s:5:"label";s:3:"D48";s:7:"casting";s:11:"05 Apr 2017";s:4:"data";a:5:{i:0;a:2:{s:1:"x";i:1;s:1:"y";s:8:"8.000000";}i:1;a:2:{s:1:"x";i:2;s:1:"y";s:9:"15.560000";}i:2;a:2:{s:1:"x";i:3;s:1:"y";s:9:"20.670000";}i:3;a:2:{s:1:"x";i:7;s:1:"y";s:9:"30.440000";}i:4;a:2:{s:1:"x";i:27;s:1:"y";s:9:"42.885000";}}}i:1;a:4:{s:2:"id";i:238;s:5:"label";s:3:"a70";s:7:"casting";s:11:"23 Jan 2017";s:4:"data";a:5:{i:0;a:2:{s:1:"x";i:1;s:1:"y";s:8:"9.560000";}i:1;a:2:{s:1:"x";i:3;s:1:"y";s:9:"18.890000";}i:2;a:2:{s:1:"x";i:4;s:1:"y";s:9:"22.890000";}i:3;a:2:{s:1:"x";i:8;s:1:"y";s:9:"31.775000";}i:4;a:2:{s:1:"x";i:28;s:1:"y";s:9:"40.220000";}}}i:2;a:4:{s:2:"id";i:240;s:5:"label";s:3:"a72";s:7:"casting";s:11:"23 Jan 2017";s:4:"data";a:6:{i:0;a:2:{s:1:"x";i:1;s:1:"y";s:8:"9.330000";}i:1;a:2:{s:1:"x";i:2;s:1:"y";s:9:"16.000000";}i:2;a:2:{s:1:"x";i:3;s:1:"y";s:9:"21.560000";}i:3;a:2:{s:1:"x";i:8;s:1:"y";s:9:"34.890000";}i:4;a:2:{s:1:"x";i:28;s:1:"y";s:9:"46.000000";}i:5;a:2:{s:1:"x";i:31;s:1:"y";s:9:"52.220000";}}}i:3;a:4:{s:2:"id";i:235;s:5:"label";s:3:"a68";s:7:"casting";s:11:"21 Jan 2017";s:4:"data";a:4:{i:0;a:2:{s:1:"x";i:2;s:1:"y";s:8:"7.560000";}i:1;a:2:{s:1:"x";i:4;s:1:"y";s:9:"15.560000";}i:2;a:2:{s:1:"x";i:10;s:1:"y";s:9:"23.890000";}i:3;a:2:{s:1:"x";i:28;s:1:"y";s:9:"32.440000";}}}s:3:"age";a:10:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:7;i:5;i:8;i:6;i:10;i:7;i:27;i:8;i:28;i:9;i:31;}}s:3:"35p";a:5:{i:0;a:4:{s:2:"id";i:424;s:5:"label";s:3:"D44";s:7:"casting";s:11:"05 Apr 2017";s:4:"data";a:5:{i:0;a:2:{s:1:"x";i:1;s:1:"y";s:8:"8.670000";}i:1;a:2:{s:1:"x";i:2;s:1:"y";s:8:"9.560000";}i:2;a:2:{s:1:"x";i:3;s:1:"y";s:9:"18.440000";}i:3;a:2:{s:1:"x";i:7;s:1:"y";s:9:"33.110000";}i:4;a:2:{s:1:"x";i:27;s:1:"y";s:9:"40.335000";}}}i:1;a:4:{s:2:"id";i:425;s:5:"label";s:3:"D45";s:7:"casting";s:11:"05 Apr 2017";s:4:"data";a:5:{i:0;a:2:{s:1:"x";i:1;s:1:"y";s:8:"8.890000";}i:1;a:2:{s:1:"x";i:2;s:1:"y";s:9:"19.560000";}i:2;a:2:{s:1:"x";i:3;s:1:"y";s:9:"26.890000";}i:3;a:2:{s:1:"x";i:7;s:1:"y";s:9:"39.780000";}i:4;a:2:{s:1:"x";i:27;s:1:"y";s:9:"49.445000";}}}i:2;a:4:{s:2:"id";i:426;s:5:"label";s:3:"D46";s:7:"casting";s:11:"05 Apr 2017";s:4:"data";a:5:{i:0;a:2:{s:1:"x";i:1;s:1:"y";s:8:"6.670000";}i:1;a:2:{s:1:"x";i:2;s:1:"y";s:9:"16.440000";}i:2;a:2:{s:1:"x";i:3;s:1:"y";s:9:"16.440000";}i:3;a:2:{s:1:"x";i:7;s:1:"y";s:9:"26.670000";}i:4;a:2:{s:1:"x";i:29;s:1:"y";s:9:"40.670000";}}}i:3;a:4:{s:2:"id";i:269;s:5:"label";s:4:"NS23";s:7:"casting";s:11:"01 Feb 2017";s:4:"data";a:3:{i:0;a:2:{s:1:"x";i:1;s:1:"y";s:8:"6.670000";}i:1;a:2:{s:1:"x";i:5;s:1:"y";s:9:"21.780000";}i:2;a:2:{s:1:"x";i:7;s:1:"y";s:9:"26.000000";}}}s:3:"age";a:7:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:5;i:4;i:7;i:5;i:27;i:6;i:29;}}s:3:"45p";a:3:{i:0;a:4:{s:2:"id";i:233;s:5:"label";s:3:"A65";s:7:"casting";s:11:"21 Jan 2017";s:4:"data";a:4:{i:0;a:2:{s:1:"x";i:2;s:1:"y";s:9:"15.110000";}i:1;a:2:{s:1:"x";i:5;s:1:"y";s:9:"32.440000";}i:2;a:2:{s:1:"x";i:10;s:1:"y";s:9:"44.665000";}i:3;a:2:{s:1:"x";i:28;s:1:"y";s:9:"55.483333";}}}i:1;a:4:{s:2:"id";i:234;s:5:"label";s:3:"a66";s:7:"casting";s:11:"21 Jan 2017";s:4:"data";a:4:{i:0;a:2:{s:1:"x";i:2;s:1:"y";s:9:"14.220000";}i:1;a:2:{s:1:"x";i:5;s:1:"y";s:9:"33.780000";}i:2;a:2:{s:1:"x";i:10;s:1:"y";s:9:"44.780000";}i:3;a:2:{s:1:"x";i:28;s:1:"y";s:9:"55.560000";}}}s:3:"age";a:4:{i:0;i:2;i:1;i:5;i:2;i:10;i:3;i:28;}}} 

回答

0

我四處打聽,終於收到了一個答案和解決方案這個。

phpfreaks Forum

基本上,該陣列被重新安排和簡化,以獲得[x]和[Y]的值。那麼[x]被添加到它自己的數組中。稍後,使用[x]作爲數組鍵以循環數據以獲取[y]值。

因爲我已經重新安排我的陣列,以簡化這個過程中,我的陣列

0

你可以試試下面的代碼,其中i循環歲以上,並與「X」中的數據值檢查

//test creating HTML 
foreach($graph as $grade => $data) 

echo '<strong>'.strtoupper($grade).'</strong></br>'; 
//start the table for each grade 
echo '<table border=1>'; 
echo '<thead>'; 
echo '<tr>'; 
echo '<th>Monitor Label</th>'; 

for($i=0;$i < count($data['age']);$i++) 
{ 
    echo '<th>'.$data['age'][$i].'D</th>'; 
} 
echo '</tr>'; 
echo '</thead>'; 
echo '<tbody>'; 

for ($i=0;$i < count($data)-1; $i++) 
{ 
    echo '<tr>'; 
    echo '<td>'.$data[$i]['label'].'</td>'; 
    for($j=0;$j < count($data['age']);$j++){ 
     foreach($data[$i]['data'] as $x) 
     { 
      if($x['x'] == $data['age'][$j]) 
      echo '<td>'.$x['y'].'</td>'; 
      else 
      echo '<td> #NA </td>'; 
     } 
    } 
    echo '</tr>'; 
    } 

    echo '</tbody>'; 
    echo '</table>'; 
} 
+0

填這張表中有很多空值 – zimorok

0

您需要循環遍歷所有$data[$i]['data']陣列的count($data['age']迭代,但每次檢查是否存在具有該發生的陣列。如果是,則打印['y']號碼,如果不是,則在表格列中打印-N/A

更換

foreach($data[$i]['data'] as $x) 
{ 
    echo '<td>'.$x['y'].'</td>'; 
} 

這隻會循環的次數,一個['data']次數確實存在

隨着

for ($y=0; $y < count($data['age']; $y++) { 
    if (isset($data[$i]['data'][$y])) { 
     if ($data[$i]['data'][$y]['x'] == $data['age'][$i]) { 
      echo '<td>'.$data[$i]['data'][$y]['y'].'</td>'; 
     } else { 
      echo '<td>N/A</td>'; 
     } 
    } else { 
     echo '<td>N/A</td>'; 
    } 
} 
+0

的它打印'NA'但數據仍放置不正確。在表格末尾添加「NA」。也許我需要重新格式化如何顯示數據表 – zimorok

+0

啊,我錯過了那個woops。讓我再想一想 – RiggsFolly

+0

看看你是否按照你的意願行事。 – RiggsFolly