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"];
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)