0

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() {

7
  • check your php version and see for anonymous functions are supported for that version or not ? Commented Dec 16, 2013 at 11:51
  • unfortunately it is not supported, 5.2.6. how do i re-write the above code to work on this version? Commented Dec 16, 2013 at 12:03
  • yes, there are two options one is to update the php version for above or change the function, although if this prob. is occurring at time of hosting then just raise a ticket to server support team to update the servers php version . Commented Dec 16, 2013 at 12:07
  • ya, that is what my real question here is, how do i change the above code to work on lower version on php like < 5.3? Commented Dec 16, 2013 at 12:12
  • create two separate functions for these anonymous functions suppose on is having name fn1 having signature as of function($aVal, $bVal) and then pass the argument by separating comma by calling like array_map(fn1, $result, $intersect); and same thing have have to be applied for second anonymous function also Commented Dec 16, 2013 at 12:22

2 Answers 2

4

Anonymous functions are not available in 5.2

See the changelog here.

5.3.0 Anonymous functions become available.

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

Comments

0
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

2 Comments

still i am getting the same problem, I am calling the function inside the foreach loop, does this has some thing to do with the error?
which one function is being called in foreach loop , although calling a function in foreach loop have not to change it's behaviour , debug your code by checking the array data and other variables being passed inside the function that is only the way you can get the solution just by tracing them on each step.

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.