基于在PHP密钥组数组值?(Group array values based on key in p

2019-07-17 17:58发布

我有一个这样的数组:

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

)

我想基于“否”作为主键阵列中的组的元素。 输出应该是这样的:

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

需要注意的是作者的价值得到了关于合并到主键“No'.Can任何人帮我从这个好吗?

我试着这样做:

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

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


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


Answer 3:

由于工匠的真棒功能,如果有人需要使用多个密钥我编辑的工匠功能到这个组:

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


文章来源: Group array values based on key in php?