如何获取数组php的一些值

时间:2017-06-01 23:04:37

标签: php arrays

我正在运行代码来获取表的列名,但我想要除第一个和第二个之外的所有值。

<select name="TableNum" class="table">
  <?php
  include 'connectDb.php';  #Eisagwgi stoixeiwn gia syndesi me ti vasi
  $result=mysqli_query($con,"SELECT COLUMN_NAME FROM 
   INFORMATION_SCHEMA.Columns where TABLE_NAME = 'available'");
  while($row = mysqli_fetch_array($result)) {
    echo '<option>'.$row[0].'</option>';
    }
  ?>                
</select>

我从Python知道如何使用数组,并且我在W3Schools中读到了这个thread

另外,通过一个简单的print_r,我得到了结果:

Array ( [0] => Date [COLUMN_NAME] => Date ) 
Array ( [0] => Time [COLUMN_NAME] => Time )
Array ( [0] => A1 [COLUMN_NAME] => A1 )
Array ( [0] => A2 [COLUMN_NAME] => A2 )
Array ( [0] => A3 [COLUMN_NAME] => A3 ) 
Array ( [0] => A4 [COLUMN_NAME] => A4 )
Array ( [0] => B1 [COLUMN_NAME] => B1 )
Array ( [0] => B2 [COLUMN_NAME] => B2 )
Array ( [0] => B3 [COLUMN_NAME] => B3 )
Array ( [0] => B4 [COLUMN_NAME] => B4 )
Array ( [0] => B5 [COLUMN_NAME] => B5 )
Array ( [0] => B6 [COLUMN_NAME] => B6 )
Array ( [0] => C1 [COLUMN_NAME] => C1 )
Array ( [0] => C2 [COLUMN_NAME] => C2 )

我怎样才能得到最后12个值(A1-A1,B1-B6,C1,C2)并将其放回选项标签。

由于

1 个答案:

答案 0 :(得分:1)

这个逻辑可能最好在SQL本身中完成,但是如果你想这样做PHP:

$result = mysqli_query($con,"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'available'");
$result = $result->fetch_all(MYSQLI_ASSOC);
echo '<option>', implode('</option><option>', array_slice(array_column($result, 'COLUMN_NAME'), 2)), '</option>';