简体   繁体   English

将 3 列的数据库结果转换为关联数组

[英]Converting a database result of 3 columns to associative array

I have a MySQL query that returns the following result:我有一个返回以下结果的 MySQL 查询:

+----------+----------+-------+
| ApptTime | ApptType | Count |
+----------+----------+-------+
| 16:00    | pickup   | 1     |
+----------+----------+-------+
| 16:30    | dropoff  | 1     |
+----------+----------+-------+
| 16:30    | pickup   | 6     |
+----------+----------+-------+
| 16:45    | pickup   | 2     |
+----------+----------+-------+
| 16:45    | dropoff  | 1     |
+----------+----------+-------+

I need to loop through the result and create a new associative array where the key is the ApptTime and the value is the combination of ApptType and Count like so:我需要遍历结果并创建一个新的关联数组,其中键是ApptTime ,值是ApptTypeCount的组合, ApptType所示:

"16:00" => "1 pickup"
"16:30" => "1 dropoff, 6 pickup" //the order in which appointments are listed is irrelevant. 
"16:45" => "1 dropoff, 2 pickup"

I've tried looping through the mysqli_fetch_assoc result in a variety of ways and creating a new array within the loop but I'm fundamentally not understanding how to go about it so it's not working out.我试过以各种方式循环遍历mysqli_fetch_assoc结果并在循环中创建一个新数组,但我从根本上不了解如何进行它,所以它不起作用。 Any help is greatly appreciated.任何帮助是极大的赞赏。

You can achieve this by using two loops.您可以通过使用两个循环来实现这一点。 One to prepare the values for the format you want, and one to actually put the values in that format.一种是为您想要的格式准备值,另一种是将值实际放入该格式中。

First, prepare the data一、准备资料

$array = array(
    array('ApptTime' => "16:00", 'ApptType' => "pickup", 'Count' => 1),
    array('ApptTime' => "16:30", 'ApptType' => "dropoff", 'Count' => 1),
    array('ApptTime' => "16:30", 'ApptType' => "pickup", 'Count' => 6),
    array('ApptTime' => "16:45", 'ApptType' => "pickup", 'Count' => 2),
    array('ApptTime' => "16:45", 'ApptType' => "dropoff", 'Count' => 1)
);

$new_array = [];
foreach($array as $arr) {

    $time = $arr['ApptTime'];
    $type = $arr['ApptType'];
    $count = $arr['Count'];

    //check if `$time` exists as key of `$new_array`, if not, create it with an empty array value
    if(!array_key_exists($time, $new_array)) {
        $new_array[$time] = [];
    }

    //add string e.g "1 pickup" as array value to array `$new_array` with key `$time`
    $new_array[$time][] = "{$count} {$type}";
}

First loop return第一次循环返回

Array
(
    [16:00] => Array
        (
            [0] => 1 pickup
        )

    [16:30] => Array
        (
            [0] => 1 dropoff
            [1] => 6 pickup
        )

    [16:45] => Array
        (
            [0] => 2 pickup
            [1] => 1 dropoff
        )

)

And this is where I would personally stop, and format the values when actually needed to be displayed.这就是我个人会停下来的地方,并在实际需要显示时格式化这些值。 However, the following loop can be used to modify the $new_array to the format you wanted.但是,可以使用以下循环将$new_array修改为您想要的格式。

foreach($new_array as &$n_arr) {
    $n_arr = implode(', ', $n_arr);
}

Second loop return第二次循环返回

Array
(
    [16:00] => 1 pickup
    [16:30] => 1 dropoff, 6 pickup
    [16:45] => 2 pickup, 1 dropoff
)

Use this logic :使用这个逻辑:

I have taken sample array assuming you are getting same in MySql resultset.假设您在 MySql 结果集中得到相同的结果,我已经采用了示例数组。

<?php

$resultset = array(
     array("16:00","pickup", 1),
     array("16:30","Drop of ", 1),
     array("16:30","pickup", 6),
     array("16:45","pickup", 2),
     array("16:45","Drop of ", 1)

    );
    $sample_array=array();
    foreach ( $resultset as $value )
   {
       //var_dump($value);
       if(!array_key_exists($value[0],$sample_array)){

           $sample_array[$value[0]] = array();
       }
       array_push($sample_array[$value[0]],$value[2]." ".$value[1]);
       //var_dump($sample_array);

   } 
   var_dump($sample_array);
?>

Output :输出 :

array(3) {
  ["16:00"]=>
  array(1) {
    [0]=>
    string(8) "1 pickup"
  }
  ["16:30"]=>
  array(2) {
    [0]=>
    string(10) "1 Drop of "
    [1]=>
    string(8) "6 pickup"
  }
  ["16:45"]=>
  array(2) {
    [0]=>
    string(8) "2 pickup"
    [1]=>
    string(10) "1 Drop of "
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM