简体   繁体   中英

Callback functions in PHP < 5.3

I have an multidimensional array and want to do usort in zendframework.**

Following code work in PHP 5.3+, but not for the lower versions because of the callback function in usort .

usort($array, function (array $a, array $b) {
    return date('Ymdhis',$a['time']) - date('Ymdhis',$b['time']); 
});

So instead of the callback function how can I divide it and use it from external function call in ZEND FRAMEWORK.

In normal PHP script individual call is working as below.

usort($array, 'usortcallback');

function usortcallback(array $a, array $b) {
    return date('Ymdhis',$a['time']) - date('Ymdhis',$b['time']);
});

But I want workable code for Zend Framework.

Thanks, Sandeep

You can use create_function() , that will fork for older PHP too.

$callback = create_function(
    '$a, $b',
    'return date("Ymdhis",$a["time"]) - date("Ymdhis",$b["time"]);'
);

usort($array, $callback);

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