dongyue7796 2015-11-20 03:42
浏览 36
已采纳

显示与数据库数据匹配的某些数组数据

I have been digging around trying to figure this out for a few days now. The issue is that I have a form that enters data into a MySQL database table and links that data to a service ID. The table contains 3 colums: id serviceid and data. I also have an array (working with a json API) that outputs all items (not linked to a service id) under data. What I am trying to do is match the service ID with the data, and then only display that specific data from the array.

In a nutshell, if I am viewing service #186, it has the 2 sets of data that I want to be able to pull from the array (not the db). It should only pull the array items ([0] and [2]) that match the table column data with the array item ["name"].

+------------+-----------+
| id| serviceid | data
+------------+-----------+
| 3 | 186 | SomeMore |
| 2 | 185 | NotEnough |
| 1 | 186 | Data5 |
+------------+-----------+

array {
  ["data_bits"]=>
  array(5) {
    [0]=>
    array(3) {
      ["name"]=> "Data5"
      ["info"]=> "some info"
      ["comment"]=> "cool beans"
    }
    [1]=>
    array(3) {
      ["name"]=> "NotEnough"
      ["info"]=> "some cool info"
      ["comment"]=> "warm beans"
    }
    [2]=>
    array(3) {
      ["name"]=> "SomeMore"
      ["info"]=> "some bad info"
      ["comment"]=> "hot beans"
    }
  }

Currently, I have the SQL query that will select the items that have the service ID.

$get = $pdo->prepare("SELECT data FROM table WHERE serviceid=:serviceid");
        $get->bindParam(':serviceid', $serviceid, PDO::PARAM_STR);
        $get->execute();
        $data_array = array();
        while ($db_data = $get->fetch()) {
            $data_array[] = $db_data[0];
        }

If I echo $data_array[0]; in foreach statement, it show all data related to the current service.

Sorry if this doesn't make a lot of sense. It's rattling me insane. If you need a better explanation, i'll do what I can to try to clarify.

  • 写回答

1条回答 默认 最新

  • duanji9311 2015-11-20 03:57
    关注

    You can search in $data_bits array to find those subarrays that have a name attribute which is one of the query results.

    $get = $pdo->prepare("SELECT data FROM table WHERE serviceid=:serviceid");
    $get->bindParam(':serviceid', $serviceid, PDO::PARAM_STR);
    $get->execute();
    $result = $get->fetchAll();
    
    $search_values = array();
    $wanted_data = array();
    
    foreach($result as $row) {
        $search_values[] = $row['data'];
    }
    
    foreach($data_bits as $key => $value) {
        if (in_array($value['data'], $search_values)) {
            $wanted_data[] = $data_bits[$key];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM