基于php中的键组数组值?

时间:2013-01-01 18:54:11

标签: php arrays merge grouping

我有一个这样的数组:

$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] = array(
                'No'=>$element['No'],
                'Paper_id' => $element['Paper_id'],
                'Title' => $element['Title'],
                'Authors' => array(),
                'Aff_list' => $element['Aff_list'],
                'Abstracts' => $element['Abstracts']
            );
        }
        $out[$id]['Authors'][] = array('Authors' => $element['Author']);
    }
}

4 个答案:

答案 0 :(得分:45)

您可以使用通用功能:

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

答案 1 :(得分:6)

您问题中的数据格式含糊不清,但假设$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');

答案 2 :(得分:1)

感谢crafter的强大功能,如果有人需要分组多个键,我将crafter函数编辑为此:

operator float*()

答案 3 :(得分:0)

感谢火山口和法比奥的回答。我更新了代码,以检查密钥的大小是否不超过一(1),不会附加下划线。

function _group_by($array, $keys=array()) {
    $return = array();
    $append = (sizeof($keys) > 1 ? "_" : null);
    foreach($array as $val){
        $final_key = "";
        foreach($keys as $theKey){
            $final_key .= $val[$theKey] . $append;
        }
        $return[$final_key][] = $val;
    }
    return $return;
}