可以';t使用array_map从两个数组中提取结果


Can't use array_map to extract results from two arrays

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

我拥有的第一个阵列是:

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

我同意@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数组,则可以从参数和回调函数位置中删除。另外,我可以看到你有不正确的牙套问题。