根据php中的键对数组值进行分组?

2022-08-30 19:26:27

我有一个这样的数组:

$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"

)

我想根据“No”作为主键对数组中的元素进行分组。输出应如下所示:

 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"
"
}

请注意,作者的值已与主键“否”合并。任何人都可以帮我解决这个问题吗?

我尝试这样做:

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']];
    }
}

答案 1

您可以使用泛型函数:

function _group_by($array, $key) {
    $return = array();
    foreach($array as $val) {
        $return[$val[$key]][] = $val;
    }
    return $return;
}

我添加了一些示例代码进行测试

<?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'));

答案 2

问题中的数据格式含糊不清,但假设其结构如下,这应该可以获得所需的输出。$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'];
}

还应在开发环境中打开警告并显示错误。我有一种感觉,它会帮助你。在开发过程中,您可以配置 php.ini,也可以将此代码插入 php 脚本的开头。只需确保在推送到生产环境之前将其删除即可。

error_reporting(E_ALL);
ini_set('display_errors', '1');