0

i have a function that strips the underscore from an array kay and replaces it with a space in order to insert into the database.

when it runs on he test server it runs fine, but when it's live it reports a syntax error on line 22 -- expecting a ")" .

here is the function:

function fixArrayKey(&$arr)
{
    $arr=array_combine(array_map(function($str){return str_replace("_"," ",$str);},array_keys($arr)),array_values($arr));
    foreach($arr as $key=>$val)
    {
        if(is_array($val)) fixArrayKey($arr[$key]);
    }
}

line 22 is where the $arr=array_combine starts.

for the life of me I can't see where it has gone wrong or even comprehend why it works on the local server but not the live one.

my live server is an IIS7 server, would that have anything to do with it?

1
  • 3
    Anonymous functions were added in PHP 5.3, do you have an earlier version on your live server? Commented Dec 19, 2012 at 15:02

1 Answer 1

1

Maybe if you made it a bit simpler you would find where the error lies

function fixArrayKey(&$arr)
{
    $keys   = array_keys($arr));
    $values = array_values($arr);

    // Try without the lambda function
    $map = array_map('lambda_replace', $keys);

    // Or you can uncoment the line below (and comment the one above) to use
    // the lambda function
    //$map = array_map(function($str){return str_replace("_"," ",$str);}, $keys);

    $arr = array_combine($map, $values);

    foreach ($arr as $key=>$val)
    {
        if(is_array($val)) fixArrayKey($arr[$key]);
    } 
}

function lambda_replace($str)
{
    return str_replace('_', ' ', $str);
}

A good idea would be to do a phpinfo() on both development and production environments to identify any differences between them. For instance modules on PHP behave differently on different versions or even PHP itself has different features from version to version.

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

1 Comment

my windows host is only php 5.2 and my local is 5.3 ... that appears to be the problem

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.