3

我有这样的数组:

{"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>';
    }

但是我想要的结果应该是这样的>>>

表格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磅|6L磅|9L磅|12L磅|15L磅|18L磅| 
2L磅|5L磅|8L磅|11L磅|14L磅|17L磅|
1L磅|4L磅|7L磅|10L磅|13L磅|16L磅|

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

4

3 回答 3

2

我已经在这里发布了代码..

http://jaiphp.blogspot.in/

这有帮助吗?

于 2012-11-22T09:25:09.327 回答
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>";
于 2012-11-23T06:37:44.073 回答
-1

您粘贴了数组的转储,我无法在我的 PC 中尝试使用它。使用 'echo json_encode($array);' 并粘贴

于 2012-11-22T06:37:31.880 回答