久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

按一列對子數組進行分組,從組內的其他列生成

Group subarrays by one column, make comma-separated values from other column within groups(按一列對子數組進行分組,從組內的其他列生成逗號分隔的值)
本文介紹了按一列對子數組進行分組,從組內的其他列生成逗號分隔的值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述


我有一個看起來像這樣的數組:


I have a array that looks like this:

$array = [
    ["444", "0081"],
    ["449", "0081"],
    ["451", "0081"],
    ["455", "2100"],
    ["469", "2100"]
];

我需要分組為一個新數組,如下所示:

I need to group as a new array that looks like:

array (
  0 => 
  array (
    0 => '444,449,451',
    1 => '0081',
  ),
  1 => 
  array (
    0 => '455,469',
    1 => '2100',
  ),
)

我嘗試了很多腳本,但都沒有成功.

I'd tried many scripts, but with no success.

function _group_by($array, $key) {
    $return = array();
    foreach($array as $val) {
        $return[$val[$key]][] = $val;
    }
    return $return;
}
$newArray = _group_by($array, 1); // (NO SUCCESS)

推薦答案

應該有更優雅的解決方案,但我能想到的最簡單的就是這個.

There should be more elegant solutions, but simplest one I can think of would be this.

// The data you have pasted in the question
$data = []; 
$groups = [];

// Go through the entire array $data
foreach($data as $item){
    // If the key doesn't exist in the new array yet, add it       
    if(!array_key_exists($item[1], $groups)){
        $groups[$item[1]] = [];
    }

    // Add the value to the array
    $groups[$item[1]][] = $item[0];
}

// Create an array for the data with the structure you requested
$structured = [];
foreach($groups as $group => $values){
    // With the array built in the last loop, implode it with a comma
    // Also add the 'key' from the last array to it ($group)
    $structured[] = [implode(',', $values), $group];
}

我還沒有測試過這個,但類似的東西應該可以解決問題.這只是通過給定的數組并以結構化的方式收集所有條目(因此 $groups 變量將包含每個共享密鑰的組的數組條目,并且該密鑰將對應于每個給定數組中的項目).從那里開始,它只是重組它以獲得您要求的格式.

I haven't tested this but something similar should do the trick. This simply goes through the given array and collects all entries in a structurized manner (so $groups variable will contain an array entry for each group sharing a key, and the key will correspond to the 2nd item in each item within the given array). From there it's just about restructuring it to get the format you have requested.

http://php.net/manual/en/control-structures.foreach.php

這篇關于按一列對子數組進行分組,從組內的其他列生成逗號分隔的值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 韩日欧美 | 一区中文字幕 | 伊人久久网站 | 在线a | 久久婷婷色| 亚洲福利精品 | 五月激情久久 | 免费网站观看www在线观看 | 午夜精品影院 | 成人在线视频观看 | 色黄视频在线观看 | 日韩精品一区二区在线 | 久操精品视频 | 欧美激情精品 | 久操不卡| 国产一区二区三区免费 | 日韩在线观看中文字幕 | 国产一区二区三区在线 | 欧美性猛交xxxx黑人猛交 | 91亚洲国产 | 日韩精品综合 | av手机版 | 成人性生活片 | 99热国产| 中文字幕在线观看亚洲 | 九九视频在线 | 亚洲一区二区在线视频 | 久久久久亚洲精品 | 欧美精品日韩少妇 | 草草在线观看 | 岛国免费av | 成人三级小说 | av手机天堂 | 欧美一区二区在线 | 性巴克成人免费网站 | 成年人免费在线视频 | 这里只有精品视频 | 91在线看片 | 国产精品99999 | 国产精品高潮呻吟 | 91美女片黄在线观看91美女 |