I am trying to change the Keys of an array and merge another array to it.
The first array I have is:
$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.
)
)
The second array that I want to merge is:
$authors = Array (
[Authors] => Array (
[0] => Array (
[0] => Array (
[author] => Array (
[first] => Komiyama
[last] => Hiroshi
)
)
)
)
)
The final output I am getting is missing the Authors:
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] =>
)
The code in question is:
$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);
I agree with @ahmed. You're passing two arguments and you're accepting one. Please, can you try with the following:
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);
If don't need to use the $authors
array then you can remove from both of the arguments and callback function location. Also, I can see you've incorrect braces issue.