doupao2277 2016-10-09 21:21
浏览 30
已采纳

如何删除sql-database-array中的选定值

By using an html-form, I want to read out data from an sql-database and show them in an html-table. This works fine! Now, I want to reduce the number of columns in the table AFTER the sql-select-query.

$field = mysqli_fetch_fields($result) returns the column-names and var_dunp($field) returns the folowing result:

array(11) { [0]=> object(stdClass)#3 (13) { ["name"]=> string(2) "id" ["orgname"]=> string(2) "id" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(1) ["length"]=> int(11) ["charsetnr"]=> int(63) ["flags"]=> int(49667) ["type"]=> int(3) ["decimals"]=> int(0) } [1]=> object(stdClass)#4 (13) { ["name"]=> string(7) "Vorname" ["orgname"]=> string(7) "Vorname" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(9) ["length"]=> int(60) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [2]=> object(stdClass)#5 (13) { ["name"]=> string(4) "Name" ["orgname"]=> string(4) "Name" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(14) ["length"]=> int(60) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [3]=> object(stdClass)#6 (13) { ["name"]=> string(3) "Str" ["orgname"]=> string(3) "Str" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(22) ["length"]=> int(60) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [4]=> object(stdClass)#7 (13) { ["name"]=> string(3) "PLZ" ["orgname"]=> string(3) "PLZ" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(4) ["length"]=> int(15) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(254) ["decimals"]=> int(0) } [5]=> object(stdClass)#8 (13) { ["name"]=> string(3) "Ort" ["orgname"]=> string(3) "Ort" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(10) ["length"]=> int(90) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [6]=> object(stdClass)#9 (13) { ["name"]=> string(3) "Tel" ["orgname"]=> string(3) "Tel" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(11) ["length"]=> int(75) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [7]=> object(stdClass)#10 (13) { ["name"]=> string(5) "EMail" ["orgname"]=> string(5) "EMail" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(20) ["length"]=> int(90) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [8]=> object(stdClass)#11 (13) { ["name"]=> string(3) "WWW" ["orgname"]=> string(3) "WWW" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(17) ["length"]=> int(90) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [9]=> object(stdClass)#12 (13) { ["name"]=> string(7) "Notizen" ["orgname"]=> string(7) "Notizen" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(13) ["length"]=> int(196605) ["charsetnr"]=> int(33) ["flags"]=> int(16) ["type"]=> int(252) ["decimals"]=> int(0) } [10]=> object(stdClass)#13 (13) { ["name"]=> string(10) "Geburtstag" ["orgname"]=> string(10) "Geburtstag" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(10) ["length"]=> int(30) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } }

Since this is an array, I now want to remove selected elements from this array and tried to do this with:

    // 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]);
           }

or with

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

However, both ways did not work. Obviously, the element I want to remove is not found within the array with this methods.

In a second step, I have to modify the while-loop, so that the correct values are shown in the respective columns:

      $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  

I would be grateful if somebody could help me with this Problem!

  • 写回答

1条回答 默认 最新

  • douhu4091 2016-10-09 22:02
    关注

    That's not normal array, that's an array of objects, which is what's expected from mysqli_fetch_fields() function, so your delete logic won't work in that fashion. Your code should be like this:

    (Suppose PLZ is the column you want to delete here)

    // Initilize what to delete
    $delete_val = array('PLZ');
    // Search for the array key and unset
    foreach($delete_val as $dv){
        foreach($field as $key => $object){
            $array = (array)$object;
            if(in_array($dv, $array, true)){
                unset($field[$key]);
            }
        }   
    }
    $field = array_values($field);
    
    // display $field array
    var_dump($field);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?