php 根据php中的键对数组值进行分组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14113256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Group array values based on key in php?
提问by Aditya
I have a array like this:
我有一个这样的数组:
$str=
Array
(
[No] => 101
[Paper_id] => WE3P-1
[Title] => "a1"
[Author] => ABC
[Aff_list] => "University of South Florida, Tampa, United States"
[Abstracts] => "SLA"
)
Array
(
[No] => 101
[Paper_id] => WE3P-1
[Title] => "a2"
[Author] => DEF
[Aff_list] => "University of South Florida, Tampa, United States"
[Abstracts] => "SLA "
)
Array
(
[No] => 104
[Paper_id] => TU5A-3
[Title] => "a3"
[Author] => GHI
[Aff_list] => "University of Alcala, Alcala de Henares, Spain"
[Abstracts] => "Microwave"
)
I want to group elements in the array based upon 'No' as primary key. The output should look like this:
我想根据“否”作为主键对数组中的元素进行分组。输出应如下所示:
array(6) {
["No"]=>
string(6) "101"
["Paper_id"]=>
string(6) "WE3P-1"
["Title"]=>
string(80) ""a-1"
["Author"]=>
string(14) "ABC"
["Aff_list"]=>
string(51) ""University of South Florida, Tampa, United States""
["Abstracts"]=>
string(5) ""(SLA)"
"
}
array(6) {
["No"]=>
string(3) "104"
["Paper_id"]=>
string(6) "TU5A-3"
["Title"]=>
string(40) "a2"
["Author"]=>
string(20) "DEF"
["Aff_list"]=>
string(48) ""University of Alcala, Alcala de Henares, Spain""
["Abstracts"]=>
string(9) ""Microwave"
"
}
Note that the Author's value got merged with respect to the primary key 'No'.Can anyone help me out from this, please?
请注意,作者的值相对于主键“No”进行了合并。有人可以帮我解决这个问题吗?
I tried doing this:
我尝试这样做:
foreach($paper_info as $element) {
foreach($element as $v) {
$id = $element['No'];
if (!isset($out[$id])) {
$out[$id] = [
'No' => $element['No'],
'Paper_id' => $element['Paper_id'],
'Title' => $element['Title'],
'Authors' => [],
'Aff_list' => $element['Aff_list'],
'Abstracts' => $element['Abstracts']
];
}
$out[$id]['Authors'][] = ['Authors' => $element['Author']];
}
}
回答by crafter
You could use a generic function:
您可以使用通用函数:
function _group_by($array, $key) {
$return = array();
foreach($array as $val) {
$return[$val[$key]][] = $val;
}
return $return;
}
I added some sample code to test
我添加了一些示例代码进行测试
<?php
$list= [
[ 'No' => 101,
'Paper_id' => 'WE3P-1',
'Title' => "a1",
'Author' => 'ABC',
'Aff_list' => "University of South Florida, Tampa, United States",
'Abstracts' => "SLA"
] ,
[ 'No' => 101,
'Paper_id' => 'WE3P-1',
'Title' => "a2",
'Author' => 'DEF',
'Aff_list' => "University of South Florida, Tampa, United States",
'Abstracts' => "SLA"
] ,
[ 'No' => 104,
'Paper_id' => 'TUSA-3',
'Title' => "a3",
'Author' => 'GH1',
'Aff_list' => "University of Alcala, Alcala de Henares, Spain",
'Abstracts' => "Microwave"
] ];
print_r(_group_by($list, 'No'));
回答by Levi
The data format in your question is ambiguous, but assuming the structure for $paper_infois what is below, this should get you the output you're looking for.
您问题中的数据格式不明确,但假设结构$paper_info如下,这应该可以为您提供所需的输出。
$paper_info = array(
array(
'No' => "101",
'Paper_id' => "WE3P-1",
'Title' =>"An Electrically-Small, 3-D Cube Antenna Fabricated with Additive Manufacturing",
'Author' => "Ibrahim Nassar",
...
),
array(
'No' => "101",
...
'Author' => "Thomas Weller",
...
)
);
$out = array();
foreach($paper_info as $paper) {
$id = $paper['No'];
if (!isset($out[$id])) {
$out[$id] = $paper;
$out[$id]['Author'] = array();
}
$out[$id]['Author'][] = $paper['Author'];
}
You should also turn on warnings and display errors in your development environment. I have a feeling it will help you. During development you can either configure your php.ini, or insert this code at the beginning of your php script. Just make sure you remove it before pushing to production.
您还应该在开发环境中打开警告并显示错误。我有一种感觉,它会帮助你。在开发过程中,您可以配置 php.ini,也可以在 php 脚本的开头插入此代码。只需确保在推送到生产之前将其删除。
error_reporting(E_ALL);
ini_set('display_errors', '1');
回答by Fabio Sirchia
Thanks to crafter for the awesome function, if someone need to group for multiple keys i edited the crafter function to this:
感谢crafter 提供了很棒的功能,如果有人需要为多个键分组,我将crafter 功能编辑为:
function _group_by($array, $keys=array()) {
$return = array();
foreach($array as $val){
$final_key = "";
foreach($keys as $theKey){
$final_key .= $val[$theKey] . "_";
}
$return[$final_key][] = $val;
}
return $return;
}

