1

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);
2
  • 2
    You're not using it correctly, if you pass 2 arrays, then you should accept 2 arrays in your callback function. Commented Dec 2, 2015 at 17:06
  • Thanks, @ahmad, I tried the suggestion by Md Mazedul Islam Khan, but still ain't getting authors in the output. Commented Dec 2, 2015 at 19:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Md Mazedul Islam Khan, I have tried your code, but still I don't get anything in authors. Could you please let me know if I am doing anything wrong?
Because you're not storing the $output array item to the $authors array. You're only assigning $output array items to simple text which isn't possible though. You can try like this: $authors['dc:identifier''] => $tag['identifier']
thanks for the tip, following code works for me: $new_array = 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' => $authors ]; }, $output, $authors);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.