PHP 从多维数组创建多个表

标签 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

I tried with this>>>>

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 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。

最佳答案

我把代码贴在这里..

http://jaiphp.blogspot.in/

这有帮助吗?

关于PHP 从多维数组创建多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13506974/

相关文章:

php - 在存储到数据库之前批准信息?

javascript - 如何从多个数组中删除?

c++ - C++中的多维数组挂起

c++ - 设置指向任意维度数组的指针?

php - 单表还是双表 - 性能更好?

php - Eloquent,从数据透视表中删除用户权限

php - 访问数组中的私有(private)值,从对象转换而来

php - 将数组分成两半,数组总和相等或近似相等

vb.net - VB.NET 中的 IndexOf 与字符串数组

php爆炸关联数组问题