-1

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?

reformed
  • 3,922
  • 9
  • 51
  • 77
Joelgullander
  • 1,376
  • 1
  • 13
  • 30
  • Can you explain what's not working properly? – FirstOne Jul 26 '16 at 13:56
  • I just a 500 internal error when uncommenting this code... @FirstOne – Joelgullander Jul 26 '16 at 13:56
  • Please, check how to [get php errors to display](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). You can also check the logs... – FirstOne Jul 26 '16 at 13:57
  • It's not `array_map` that's the problem, is that *anonymous functions* weren't around in 5.2. You can try to use `create_function` instead, or you can try to stop using ancient and dead PHP versions (if that's a possibility). Though you will likely encounter many many more issues when running code which was designed for 5.3+ on 5.2. – deceze Jul 26 '16 at 14:00
  • Do you need to call a static method from array_map ? If so, this will not work. – Marc Giroux Jul 26 '16 at 14:00
  • 2
    PHP 5.2? Seriously? Have you any idea how old that is? That version of PHP was superseded in 2009. There have been numerous **major** changes in functionality between 5.2 and the current versions, including some major changes to default behaviour. If you're writing good quality code in PHP 5.6, you stand virtually zero chance of it running successfully in 5.2. You'd have to drop virtually all the current best-practice programming standards in order to work successfully in 5.2. Not to mention all the known security holes in 5.2; your site will be insecure as soon it goes online. – Simba Jul 26 '16 at 14:08

1 Answers1

6

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.

axiac
  • 56,918
  • 8
  • 77
  • 110
  • 2
    This is a good answer. But I really hope the OP upgrades rather than trying to use the code solution you've given him. The chances are he's going to have a whole lot more code changes than just this to make before his application will actually work, and there really is no excuse at all for still running PHP 5.2 today, especially for new code. (you can just about be excused if you have an old app that can't be upgraded, but even then the security issues and compatibility issues of running such old software should be enough to push you toward upgrading by now) – Simba Jul 26 '16 at 14:14