无法使用array_map从两个数组中提取结果

时间:2015-12-02 16:54:15

标签: php arrays array-map

我正在尝试更改数组的键并将另一个数组合并到它。

我拥有的第一个数组是:

$output = Array ( 
[0] => Array ( 
       [identifier] => doi:10.1007/978-4-431-54559-0_3 
       [url] => Array ( 
                       [0] => Array ( 
                                     [format] => 
                                     [platform] => 
                                     [value] => http://dx.doi.org/10.1007/978-4-431-54559-0_3 
                              ) 
                ) 
       [title] => “Vision 2050” to the Rescue of a “Limited Earth” 
       [creators] => Array ( 
                            [0] => Array ( 
                                          [creator] => Komiyama, Hiroshi 
                                   ) 
                     ) 
       [publicationName] => Beyond the Limits to Growth 
       [openaccess] => true 
       [doi] => 10.1007/978-4-431-54559-0_3 
       [printIsbn] => 978-4-431-54558-3 
       [electronicIsbn] => 978-4-431-54559-0 
       [isbn] => 978-4-431-54558-3 
       [publisher] => Springer 
       [publicationDate] => 2103-11-14 
       [volume] => 
       [number] => 
       [startingPage] => 
       [copyright] => ©2014 The Editor(s) (if applicable) and the Author(s)
       [genre] => OriginalPaper 
       [abstract] => AbstractNext let us consider the second paradigm—“The Limited Earth.” The problems caused by the fact that the Earth is limited are far-reaching. These include not only energy, resources, global warming, air pollution, water pollution, ground pollution, food, and water, but also—if we think broadly—such problems as the widescale spread of infectious diseases of people and livestock. The reason is that the probability of virus mutation and transmission increases along with the probability that wild animals come into contact with livestock, livestock with other livestock, humans with livestock, and so on. And in turn, the probability of contact on the limited surface of the Earth increases in proportion to the square of the population density. 
       )
)

我要合并的第二个数组是:

$authors = Array ( 
[Authors] => Array ( 
                    [0] => Array ( 
                                  [0] => Array ( 
                                                [author] => Array ( 
                                                                   [first] => Komiyama 
                                                                   [last] => Hiroshi 
                                                            ) 
                                         ) 
                           )
             )
)

我得到的最终输出缺少作者:

Array ( 
       [0] => Array ( 
                     [dc:identifier] => doi:10.1007/978-4-431-54559-0_3   
                     [dc:url] => Array ( 
                                        [0] => Array ( 
                                                      [format] => 
                                                      [platform] => 
                                                      [value] => http://dx.doi.org/10.1007/978-4-431-54559-0_3
                                               ) 
                                 ) 
                     [dc:title] => “Vision 2050” to the Rescue of a “Limited Earth” 
                     [prism:publicationName] => Beyond the Limits to Growth 
                     [dc:description] => AbstractNext let us consider the second paradigm—“The Limited Earth.” The problems caused by the fact that the Earth is limited are far-reaching. These include not only energy, resources, global warming, air pollution, water pollution, ground pollution, food, and water, but also—if we think broadly—such problems as the widescale spread of infectious diseases of people and livestock. The reason is that the probability of virus mutation and transmission increases along with the probability that wild animals come into contact with livestock, livestock with other livestock, humans with livestock, and so on. And in turn, the probability of contact on the limited surface of the Earth increases in proportion to the square of the population density. 
                     [prism:doi] => 10.1007/978-4-431-54559-0_3 
                     [authors] => 
)

有问题的代码是:

$new_array = array_map(function($tag) {
            return array(
            'dc:identifier' => $tag['identifier'],
            'dc:url' => $tag['url'],
            'dc:title' => $tag['title'],
            'prism:publicationName' => $tag['publicationName'],
            'dc:description' => $tag['abstract'],
            'prism:doi' => $tag['doi'],
            'authors' => $tag['Authors']
        ); }, $output, $authors);

1 个答案:

答案 0 :(得分:1)

我同意@ahmed。你传递了两个论点并且你接受了一个。请你试试以下内容:

array_map(function($tag, $authors) {
    return [
        'dc:identifier' => $tag['identifier'],
        'dc:url' => $tag['url'],
        'dc:title' => $tag['title'],
        'prism:publicationName' => $tag['publicationName'],
        'dc:description' => $tag['abstract'],
        'prism:doi' => $tag['doi'],
        'authors' => $tag['Authors']
    ];
}, $output, $authors);

如果不需要使用$authors数组,则可以从两个参数和回调函数位置中删除。此外,我可以看到你的大括号问题。

相关问题