MR.Internet
MR.Internet

Reputation: 645

php anonymous function throw Error on php 5.2

I have the following code.

$resu = array_map(function($aVal, $bVal){
return "$bVal [$aVal]";
}, $result, $intersect);

$sorAr = array();
array_walk($resu, function($element) use (&$sorAr) {
$parts = explode(" ", $element);
$sorAr[$parts[0]] = trim($parts[1], ' []');
});

The problem lies when i need to use the anonymous function in both variable $resu and on array_walk. the error shows as follows

Parse error: syntax error, unexpected T_FUNCTION in /dir...

I try to read on this site different suggestion but no luck. how do i solve this problem. Some one help please?

I have tried this code...

function arrSwap() { 
                        $arraySwap = function($aVal, $bVal){
                            return "$bVal [$aVal]";
                            };                         
                        $resu = array_map($arraySwap, $result, $intersect);
                        }

                        $sorAr = array();
                        function arrSwap2() {
                        $arrayWalk = function($element) use (&$sorAr) {
                            $parts = explode(" ", $element);
                            $sorAr[$parts[0]] = trim($parts[1], ' []');
                        }; 
                        array_walk($resu, $arrayWalk);
                        }

but i get this error...

Fatal error: Cannot redeclare arrSwap() (previously declared in on line 100... which the line 100 is this -> function arrSwap() {

Upvotes: 0

Views: 205

Answers (2)

floriank
floriank

Reputation: 25698

Anonymous functions are not available in 5.2

See the changelog here.

5.3.0 Anonymous functions become available.

Upvotes: 4

Abhishek
Abhishek

Reputation: 1561

function arr1($aVal, $bVal){ return "$bVal [$aVal]"; } 
function arrayWalk($element){ $parts = explode(" ", $element); $sorAr[$parts[0]] = trim($parts[1], ' []'); } 
function arrSwap(){ $resu = array_map('arr1', $result, $intersect); $sorAr = array(); array_walk($resu, 'arrayWalk'); } 

if still prob there let me know all these values being passed there

Upvotes: 0

Related Questions