第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 MySQL 計數(shù)添加到 PHP 數(shù)組值中

將 MySQL 計數(shù)添加到 PHP 數(shù)組值中

PHP
犯罪嫌疑人X 2021-06-13 16:13:54
我一直在嘗試列出帶有城市的州組以及每個城市的位置數(shù)量。有點像下面。TexasAustin (5)Dallas (8)Houston (3)除了計算城市的數(shù)量并像上面一樣顯示它之外,我一切都在進行。$sql ="SELECT DISTINCT  city,  state,  stateAbv,COUNT(CASE WHEN open = 'Y' THEN 1 END) AS cntFROM $tbl_nameWHERE open = 'Y'GROUP BY  city,  state,  stateAbvORDER BY  state;";$result = mysqli_query($conn,$sql);$num_columns = 1;$rows = array(); $k=0; while($row = mysqli_fetch_assoc($result)){    $state = $row['state'];    $stateAbv = $row['stateAbv'];    $city = $row['city'];    //Count of stores in every city    $values = mysqli_fetch_assoc($result);     $numStores = $values['cnt'];     if(!isset($rows[$row['state']])){        $rows[$row['state']] = array();    }    $rows[$row['state']][] = $city;}foreach($rows as $state => $cities){    echo '<b>'. $state .'</b>';    $cityChunks = array_chunk ($cities, $num_columns);     sort($cityChunks);     foreach($cityChunks as $row){        for($i=0; $i<$num_columns; $i++){            $city = isset($row[$i]) ? $row[$i] : "";            if ($k<3){                echo "$city($numStores)";            }            $k++;        }      }    $k=0;}$rows通過將城市放入其中,我的陣列現(xiàn)在看起來像這樣,但是我無法獲得城市并一起計數(shù)并正確顯示它。Array (   [Alabama] => Array    (        [0] => Mobile        [1] => Auburn        [2] => Hoover        [3] => Foley    )  )
查看完整描述

1 回答

?
喵喔喔

TA貢獻1735條經(jīng)驗 獲得超5個贊

你$numStores沒有被傳遞到$rows數(shù)組。一旦你有了它,你就可以array_column()用來獲取每個州的所有位置,然后array_sum()用來獲取總和。


while($row = mysqli_fetch_assoc($result)){

    $state = $row['state'];

    $stateAbv = $row['stateAbv'];

    $city = $row['city'];

    $numStores = $values['cnt']; 


    if (!isset($rows[$row['state']])){

        $rows[$row['state']] = array();

    }


    $rows[$row['state']][] = ['city' => $city, 'numStores' => $numStores];

}


foreach($rows as $state => $cities){

    echo $state." (".array_sum(array_column($cities, 'numStores')).")\n";

}


查看完整回答
反對 回復 2021-06-25
  • 1 回答
  • 0 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號