PHP从多维数组创建多个表

时间:2012-11-22 06:27:05

标签: php arrays multidimensional-array

我有这样的数组:

{"seats":[{"column":"8","fare":"995.0","name":"U10 UB","row":"0","zIndex":"1"},
{"column":"8","fare":"995.0","name":"U9 UB","row":"1","zIndex":"1"},
{"column":"8","fare":"995.0","name":"L10 LB","row":"0","zIndex":"0"},
{"column":"8","fare":"995.0","name":"L9 LB","row":"1","zIndex":"0"},
{"column":"8","fare":"995.0","name":"SL5 LB","row":"3","zIndex":"0"},
{"column":"8","fare":"995.0","name":"SU5 UB","row":"3","zIndex":"1"},
{"column":"6","fare":"995.0","name":"U8 UB","row":"0","zIndex":"1"},
{"column":"6","fare":"995.0","name":"U7 UB","row":"1","zIndex":"1"},
{"column":"6","fare":"995.0","name":"L8 LB","row":"0","zIndex":"0"},
{"column":"6","fare":"995.0","name":"L7 LB","row":"1","zIndex":"0"},
{"column":"6","fare":"995.0","name":"SL4 LB","row":"3","zIndex":"0"},
{"column":"6","fare":"995.0","name":"SU4 UB","row":"3","zIndex":"1"},
{"column":"4","fare":"995.0","name":"U6 UB","row":"0","zIndex":"1"},
{"column":"4","fare":"995.0","name":"U5 UB","row":"1","zIndex":"1"},
{"column":"4","fare":"995.0","name":"L6 LB","row":"0","zIndex":"0"},
{"column":"4","fare":"995.0","name":"L5 LB","row":"1","zIndex":"0"},
{"column":"4","fare":"995.0","name":"SL3 LB","row":"3","zIndex":"0"},
{"column":"4","fare":"995.0","name":"SU3 UB","row":"3","zIndex":"1"},
{"available":"false","column":"2","fare":"995.0","name":"U4 UB","row":"0","zIndex":"1"},
{"available":"false","column":"2","fare":"995.0","name":"U3 UB","row":"1","zIndex":"1"},
{"available":"false","column":"2","fare":"995.0","name":"L4 LB","row":"0","zIndex":"0"},
{"available":"false","column":"2","fare":"995.0","name":"L3 LB","row":"1","zIndex":"0"},
{"available":"false","column":"2","fare":"995.0","name":"SL2 LB","row":"3","zIndex":"0"},
{"available":"false","column":"2","fare":"995.0","name":"SU2 UB","row":"3","zIndex":"1"},
{"available":"false","column":"0","fare":"995.0","name":"U2 UB","row":"0","zIndex":"1"},
{"available":"false","column":"0","fare":"995.0","name":"U1 UB","row":"1","zIndex":"1"},
{"available":"false","column":"0","fare":"995.0","name":"L2 LB","row":"0","zIndex":"0"},
{"available":"false","column":"0","fare":"995.0","name":"L1 LB","row":"1","zIndex":"0"},
{"available":"false","column":"0","fare":"995.0","name":"SL1 LB","row":"3","zIndex":"0"},
{"available":"false","column":"0","fare":"995.0","name":"SU1 UB","row":"3","zIndex":"1"}]}

Array (
      [0] => Array (
        [0] => Array (
            [1] => stdClass Object (
                [column] => 0
                [name] => U2 UB
                [row] => 0
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 0
                [name] => L2 LB
                [row] => 0
                [zIndex] => 0
              )
          )
        [8] => Array (
            [1] => stdClass Object (
                [column] => 8
                [name] => U10 UB
                [row] => 0
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 8
                [name] => L10 LB
                [row] => 0
                [zIndex] => 0
              )
          )
        [6] => Array (
            [1] => stdClass Object (
                [column] => 6
                [name] => U8 UB
                [row] => 0
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 6
                [name] => L8 LB
                [row] => 0
                [zIndex] => 0
              )
          )
        [4] => Array (
            [1] => stdClass Object (
                [column] => 4
                [name] => U6 UB
                [row] => 0
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 4
                [name] => L6 LB
                [row] => 0
                [zIndex] => 0
              )
          )
        [2] => Array (
            [1] => stdClass Object (
                [column] => 2
                [name] => U4 UB
                [row] => 0
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 2
                [name] => L4 LB
                [row] => 0
                [zIndex] => 0
              )
          )
      )
      [1] => Array (
        [0] => Array (
            [0] => stdClass Object (
                [column] => 0
                [name] => L1 LB
                [row] => 1
                [zIndex] => 0
              )
            [1] => stdClass Object (
                [column] => 0
                [name] => U1 UB
                [row] => 1
                [zIndex] => 1
              )
          )
        [8] => Array (
            [1] => stdClass Object (
                [column] => 8
                [name] => U9 UB
                [row] => 1
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 8
                [name] => L9 LB
                [row] => 1
                [zIndex] => 0
              )
          )
        [6] => Array (
            [1] => stdClass Object (
                [column] => 6
                [name] => U7 UB
                [row] => 1
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 6
                [name] => L7 LB
                [row] => 1
                [zIndex] => 0
              )
          )
        [4] => Array (
            [1] => stdClass Object (
                [column] => 4
                [name] => U5 UB
                [row] => 1
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 4
                [name] => L5 LB
                [row] => 1
                [zIndex] => 0
              )
          )
        [2] => Array (
            [1] => stdClass Object (
                [column] => 2
                [name] => U3 UB
                [row] => 1
                [zIndex] => 1
              )
            [0] => stdClass Object (
                [column] => 2
                [name] => L3 LB
                [row] => 1
                [zIndex] => 0
              )
          )
      )
      [3] => Array (
        [8] => Array (
            [0] => stdClass Object (
                [column] => 8
                [name] => SL5 LB
                [row] => 3
                [zIndex] => 0
              )
            [1] => stdClass Object (
                [column] => 8
                [name] => SU5 UB
                [row] => 3
                [zIndex] => 1
              )
          )
        [6] => Array (
            [0] => stdClass Object (
                [column] => 6
                [name] => SL4 LB
                [row] => 3
                [zIndex] => 0
              )
            [1] => stdClass Object (
                [column] => 6
                [name] => SU4 UB
                [row] => 3
                [zIndex] => 1
              )
          )
        [4] => Array (
            [0] => stdClass Object (
                [column] => 4
                [name] => SL3 LB
                [row] => 3
                [zIndex] => 0
              )
            [1] => stdClass Object (
                [column] => 4
                [name] => SU3 UB
                [row] => 3
                [zIndex] => 1
              )
          )
        [2] => Array (
            [0] => stdClass Object (
                [column] => 2
                [name] => SL2 LB
                [row] => 3
                [zIndex] => 0
              )
            [1] => stdClass Object (
                [column] => 2
                [name] => SU2 UB
                [row] => 3
                [zIndex] => 1
              )
          )
        [0] => Array (
            [0] => stdClass Object (
                [column] => 0
                [name] => SL1 LB
                [row] => 3
                [zIndex] => 0
              )
            [1] => stdClass Object (
                [column] => 0
                [name] => SU1 UB
                [row] => 3
                [zIndex] => 1
              )
          )
      )
    )

我尝试从上面的数组中创建两个表,结果为:

L2 LB
U2 UB
L4 LB
U4 UB
L6 LB
U6 UB
L8 LB
U8 UB
L10 LB
U10 UB
L1 LB
U1 UB
L3 LB
U3 UB
L5 LB
U5 UB
L7 LB
U7 UB
L9 LB
U9 UB
SL1 LB
SU1 UB
SL2 LB
SU2 UB
SL3 LB
SU3 UB
SL4 LB
SU4 UB
SL5 LB
SU5 UB

我尝试使用此>>>>

foreach ($output_sub1 as $output_sub11) {
         ksort($output_sub11);
         echo '<table border="1">'; 
        foreach ($output_sub11 as $output_sub12) {
          echo '<tr>';
         //print_r($output_sub12);  
         ksort($output_sub12);        

        foreach($output_sub12 as $row_sub1 => $columns_sub1) {
             echo '<td>';
                echo  $columns_sub1->name."<br>";                 
             echo '</td>';    
                }
        echo '</tr>';
            }
        echo '</table>';
    }

但我想要的结果应该是这样的&gt;&gt;&gt;

表#1:

3U UB |6U UB |9U UB |12U UB |15U UB |18U UB |
2U UB |5U UB |8U UB |11U UB |14U UB |17U UB |
1U UB |4U UB |7U UB |10U UB |13U UB |16U UB |

表#2:

3L LB |6L LB |9L LB |12L LB |15L LB |18L LB |
2L LB |5L LB |8L LB |11L LB |14L LB |17L LB |
1L LB |4L LB |7L LB |10L LB |13L LB |16L LB |

有谁能说我如何遍历循环来创建如上表? 注意:数组包含zIndex值为0和1.因此,所有zIndex值0应分组为表#1,所有zIndex值1应分组为表#2。

3 个答案:

答案 0 :(得分:2)

我在这里发布了代码..

http://jaiphp.blogspot.in/

这有帮助吗?

答案 1 :(得分:0)

试试这个,

$json_string = "CONTAINS THE JSON OUTPUT"           
            $allseats = json_decode($json_string);
            $table1 = array();
            //print_r($allseats);exit;
            foreach ($allseats->seats as $seats) {
            //    print_r($seats); 
            if($seats->zIndex == 1) {
                $table1[] = $seats; 
            } else {
                $table2[] = $seats; 
            }
            }




            foreach ($table1 as $data) {
            $datarow[$data->row][$data->column] = $data->name;
            }

            foreach ($table2 as $data2) {
            $datarow2[$data2->row][$data2->column] = $data2->name;
            }
            echo "<table border=1>";
            foreach ($datarow as $row) {
            echo "<tr>";
                            //Please change the "i" value as per your requirement
            for($i=0;$i<20;$i++) {
                if (isset($row[$i])) {
                echo "<td>";
                echo $row[$i];
                echo "</td>";
                }
            }
            echo "</tr>";
            }
            echo "</table>";


            echo "<table border=1>";
            foreach ($datarow2 as $row2) {
            echo "<tr>";
            for($i=0;$i<20;$i++) {
                if (isset($row2[$i])) {
                echo "<td>";
                echo $row2[$i];
                echo "</td>";
                }
            }
            echo "</tr>";
            }
            echo "</table>";

答案 2 :(得分:-1)

你粘贴了阵列的转储,我不能在我的电脑上尝试这个。使用'echo json_encode($ array);'并粘贴