简体   繁体   中英

Alternative for array_map PHP 5.2

Something is wrong and I suspect it to be my PHP version that's 5.2 on this server; code was running on 5.6 before without any flaws...

I have debugged it down to the following code that's breaking. However, I get no error message..

    $standard = array_map( function( $item ) {
        return $item['standard_resolution']->url;
    }, $images );

Can anyone help me redo this part of code so that it works in 5.2?

There is nothing wrong with array_map() on PHP 5.2.

The problem is in your code: it uses anonymous functions but they were introduced in PHP 5.3 (see the Changelog section at the bottom of the documentation page).

In order to run this code on PHP 5.2 (or earlier) you have to use the create_function() function to create an anonymous PHP function:

 $standard = array_map(
     create_function('$item', 'return $item["standard_resolution"]->url;'),
     $images
 );

However, if possible, it's much better to upgrade your PHP interpreter to version 7.0 or 5.6. PHP 5.2 is dead and buried more than 5 years ago.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM