在php中基于键的数组值组?

时间:2022-04-19 22:07:11

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

2 个解决方案

#1


34  

You could use a generic function:

您可以使用一个通用函数:

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

#2


5  

The data format in your question is ambiguous, but assuming the structure for $paper_info is 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。在php脚本的开头插入这段代码。在投入生产之前,一定要将其删除。

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

#1


34  

You could use a generic function:

您可以使用一个通用函数:

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

#2


5  

The data format in your question is ambiguous, but assuming the structure for $paper_info is 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。在php脚本的开头插入这段代码。在投入生产之前,一定要将其删除。

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