如何删除sql-database-array

时间:2016-10-09 21:21:28

标签: php html mysql

通过使用html表单,我想从sql-database读出数据并在html表中显示它们。这很好用!现在,我希望在sql-select-query之后减少表中的列数。

$field = mysqli_fetch_fields($result)返回列名,var_dunp($field)返回以下结果:

  

array(11){[0] => object(stdClass)#3(13){[" name"] => string(2)" id" [" ORGNAME"] => string(2)" id" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(1)[" length"] => int(11)[" charsetnr"] => int(63)[" flags"] => int(49667)[" type"] => int(3)["小数"] => int(0)} [1] => object(stdClass)#4(13){[" name"] => string(7)" Vorname" [" ORGNAME"] => string(7)" Vorname" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(9)[" length"] => int(60)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(253)["小数"] => int(0)} [2] => object(stdClass)#5(13){[" name"] => string(4)" Name" [" ORGNAME"] => string(4)" Name" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(14)[" length"] => int(60)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(253)["小数"] => int(0)} [3] => object(stdClass)#6(13){[" name"] => string(3)" Str" [" ORGNAME"] => string(3)" Str" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(22)[" length"] => int(60)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(253)["小数"] => int(0)} [4] => object(stdClass)#7(13){[" name"] => string(3)" PLZ" [" ORGNAME"] => string(3)" PLZ" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(4)[" length"] => int(15)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(254)["小数"] => int(0)} [5] => object(stdClass)#8(13){[" name"] => string(3)" Ort" [" ORGNAME"] => string(3)" Ort" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(10)[" length"] => int(90)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(253)["小数"] => int(0)} [6] => object(stdClass)#9(13){[" name"] => string(3)" Tel" [" ORGNAME"] => string(3)" Tel" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(11)[" length"] => int(75)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(253)["小数"] => int(0)} [7] => object(stdClass)#10(13){[" name"] =>字符串(5)"电子邮件" [" ORGNAME"] =>字符串(5)"电子邮件" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(20)[" length"] => int(90)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(253)["小数"] => int(0)} [8] => object(stdClass)#11(13){[" name"] =>字符串(3)" WWW" [" ORGNAME"] =>字符串(3)" WWW" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(17)[" length"] => int(90)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(253)["小数"] => int(0)} [9] => object(stdClass)#12(13){[" name"] =>字符串(7)" Notizen" [" ORGNAME"] =>字符串(7)" Notizen" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(13)[" length"] => int(196605)[" charsetnr"] => int(33)[" flags"] => int(16)[" type"] => int(252)["小数"] => int(0)} [10] => object(stdClass)#13(13){[" name"] => string(10)" Geburtstag" [" ORGNAME"] => string(10)" Geburtstag" ["表"] => string(8)" adressen" [" orgtable"] => string(8)" adressen" [" DEF"] => string(0)"" ["分贝"] => string(4)" team" ["目录"] => string(3)" def" [" MAX_LENGTH"] => int(10)[" length"] => int(30)[" charsetnr"] => int(33)[" flags"] => int(0)[" type"] => int(253)["小数"] => int(0)}}

由于这是一个数组,我现在想要从这个数组中删除所选元素,并尝试使用:

    // Initilize what to delete
        $delete_val = array('PLZ');
    //     Search for the array key and unset
           foreach($delete_val as $key){
              $keyToDelete = array_search($key, $field);
              unset($field[$keyToDelete]);
           }

      $toremove = "PLZ";
      $field = array_flip($field);
      unset($field[$toremove]);
      $field = array_flip($field);

但是,两种方式都不起作用。显然,使用这种方法在数组中找不到我想要删除的元素。

在第二步中,我必须修改while循环,以便在相应的列中显示正确的值:

      $nr = 1;
      while ($row = mysqli_fetch_array($result)) {
      echo "<td>" . $nr . "</td>";
        for ($k=0; $k<$numcols; $k++) {    
        echo "<td>" . $row[$k] . "</td>"; //Prints the data
        }
      echo "</tr>";
      $nr = $nr + 1;
      }     // ende of while-loop  

如果有人能帮我解决这个问题,我将不胜感激!

1 个答案:

答案 0 :(得分:0)

那不是普通的数组,它是一个对象数组,这是mysqli_fetch_fields()函数所期望的,所以你的删除逻辑赢了& #39;以那种方式工作。你的代码应该是这样的:

(假设std::thread是您要在此处删除的列)

PLZ