简体   繁体   中英

Multi Array From Query

I am doing a count and sum on each item, and need the array to look like this.

[Date] => 
         [device_type1] => [all the data]
         [device_type2] => [all the data]

Thing is I am doing a search through each and then all of them return a date, I need all of them to be under that one date, and inside that array to hold each of the devices as key and inside that all the information. here is what I have so far.

$all_device_types = array('pole','covert','firewatch','internet','trailer','slave','project');
foreach ($all_device_types as $adt) {
    $du->select("CONCAT(YEAR(uptime_date),'-',MONTH(uptime_date),'-',DAY(uptime_date)) as year_month_day,

                             SUM(uptime_router) as router,

                             COUNT(uptime_router) as router_count,

                             uptime_device_type as device_type,

                            date(uptime_date) as data_date","

                            WHERE uptime_device_type = '".$adt."'

                            GROUP BY CONCAT(YEAR(uptime_date),'-',MONTH(uptime_date),'-',DAY(uptime_date))

                            ORDER BY uptime_date ASC");


                            $all_results = array();
                            while( $all_row = $du->fetch() ){

                                if ($adt == $all_device_types[0]) {
                                    $dates[] = $all_row['data_date'];
                                    $adt_0 = $all_row;
                                } elseif ($adt == $all_device_types[1]) {
                                    $adt_1[] = $all_row;
                                } elseif ($adt == $all_device_types[2]) {
                                    $adt_2[] = $all_row;
                                } elseif ($adt == $all_device_types[3]) {
                                    $adt_3[] = $all_row;
                                } elseif ($adt == $all_device_types[4]) {
                                    $adt_4[] = $all_row;
                                } elseif ($adt == $all_device_types[5]) {
                                    $adt_5[] = $all_row;
                                } elseif ($adt == $all_device_types[6]) {
                                    $adt_6[] = $all_row;
                                }

                            }

                        }

One of the results looks like this.

 [pole] => Array
        (
            [year_month_day] => 2017-9-5
            [router] => 4408
            [router_count] => 4620
            [device_type] => pole
            [data_date] => 2017-09-05
        )

It looks like it could be simplified quite a bit. Instead of looping over the device types and running a separate query for each one, you can just add the device type to your GROUP BY and get all your results in a single query.

I think CONCAT(YEAR(uptime_date),'-',MONTH(uptime_date),'-',DAY(uptime_date)) should output the same thing as date(uptime_date) , and you should be able to use the aliases in your GROUP BY and ORDER BY clauses.

$du->select("DATE(uptime_date) as data_date,
             uptime_device_type as device_type,
             SUM(uptime_router) as router,
             COUNT(uptime_router) as router_count,
             GROUP BY data_date, device_type
             ORDER BY data_date, device_type");

If I'm mistaken and you do need the CONCAT version, add it back in of course, but the main thing is adding device_type to the GROUP BY and eliminating the WHERE clause.

Then as you fetch your results, you can insert them directly into the multidimensional structure you want.

while( $row = $du->fetch() ) {
    $all_results[$row['data_date']][$row['device_type']][] = $row;

    // to calculate totals for all the devices for each date
    if (!isset($all_results[$row['data_date']]['all_devices']) {
        // initialize if not set
        $all_results[$row['data_date']]['all_devices'] = [
            'router' => 0, 'router_count' => 0];
    }
    // increment values
    $all_results[$row['data_date']]['all_devices']['router'] += $row['router'];
    $all_results[$row['data_date']]['all_devices']['router_count'] += $row['router_count'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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