dpbfb7119 2015-07-16 13:39
浏览 25
已采纳

从多维数组和循环访问数据

I'm struggling with extracting data from nested arrays.

Here is where I'm at with my code:

$active_tsquery = db_select("SELECT * FROM timesheets WHERE     status=\"cand\" OR status=\"client\" ORDER BY weekending ASC"); 
var_dump($active_tsquery);

foreach($active_tsquery as $key => $value) {
    $candid[] = $active_tsquery[$key]["candid"];
    $clientid[] = $active_tsquery[$key]["clientid"];
}

The db_select function for reference:

function db_select($query) {
    $rows = array();
    $result = db_query($query);

    // If query failed, return `false`
    if($result === false) {
        return false;
    }

    // If query was successful, retrieve all the rows into an array
    while ($row = mysqli_fetch_assoc($result)) {
        $rows[] = $row;
    }
    return $rows;
}

The db_query function for reference:

function db_query($query) {
    // Connect to the database
    $connection = db_connect();

    // Query the database
    $result = mysqli_query($connection,$query);

    return $result;
}

The var_dump is good - everything is being returned from the database.

array (size=15)
  0 => 
    array (size=29)
  'ts_id' => string '5453' (length=4)
  'clientid' => string '503' (length=3)
  'candid' => string '714' (length=3)
  'weekending' =>
  'department' =>
  'orderno' =>
  'monhrs' =>
  'tueshrs' =>
  'wedshrs' =>
  'thurshrs' =>)
  'frihrs' =>
  'sathrs' =>
  'sunhrs' =>
  'totalhrs' =>
  'basichrs' =>
  'othrs' =>
  'ot2hrs' =>
  'basicpay' =>
  'basiccharge' =>
  'otpay' =>
  'otcharge' =>
  'ot2pay' =>
  'ot2charge' =>
  'authname' =>
  'authdate' =>
  'ip' =>
  'status' =>
  'hue' =>
  'huc' =>

What I'm trying to do is loop through each of the id's from $active_tsquery (15 of them) in $candid, run the query below and store the results in an array for use later. Should I be using another foreach loop within the first?

"SELECT * FROM timesheetlogin WHERE id="

Thanks for any advice

  • 写回答

1条回答 默认 最新

  • doupaxia2478 2015-07-16 14:40
    关注

    Got it sorted using the following:

    $active_tsquery = db_select("SELECT * FROM timesheets WHERE status=\"cand\" OR status=\"client\" ORDER BY weekending ASC"); 
        //var_dump($active_tsquery);
    
        foreach($active_tsquery as $key => $value) {
            $clientid = $value["clientid"];
            $weekend = $value["weekending"];
            $clientresult = db_select("SELECT * FROM timesheetlogin WHERE id=\"$clientid\"");
            $candid = $value["candid"];
            $candresult = db_select("SELECT * FROM timesheetlogin WHERE id=\"$candid\"");
    
            foreach ($clientresult as $key => $value) {
                $company = $value["company"];
            }
            foreach ($candresult as $key => $value) {
                $candidate = $value["name"];
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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知识嘛?