Group array values based on key in php?

前端 未结 4 859
囚心锁ツ
囚心锁ツ 2020-11-29 06:16

I have a array like this:

$str=
   Array
(
    [No] => 101
    [Paper_id] => WE3P-1
    [Title] => \"a1\"
    [Author] => ABC
    [Aff_list] =>         


        
4条回答
  •  [愿得一人]
    2020-11-29 06:28

    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 = 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.

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

提交回复
热议问题