-2

Possible Duplicate:
Parse error: syntax error, unexpected T_FUNCTION line 10 ? help?
Reference - What does this error mean in PHP?

This is the code which cause the error.

$remaining = array_filter($allmodels, function ($var) use ($existmodels) {
                return !in_array($var, $existmodels);
        });

Logic of the code is(below all are arrays)

$remaining = $allmodels - $existmodels;

I think My PHP version is outdated in the server. Is it the problem. How can I create a similar code snippet ?

Thanks

2
  • (reference) php.net/callbacks Commented Oct 25, 2012 at 8:22
  • What's your hosting service PHP version? If it's too outdated, you may consider migrating. Starting a new project on 5.2 or older is not worth the effort. Commented Oct 25, 2012 at 8:22

3 Answers 3

3

Note: of course global isn't a good pratice

function fil($var) {
 global $existmodels;
 return !in_array($var, $existmodels);
}

$remaining = array_filter($allmodels, 'fil');
Sign up to request clarification or add additional context in comments.

1 Comment

He said he is using old PHP version :(
2

I am a big fan of OO programming, so just for the fun:

class MyArrayOperations
{
  private $base;

  public function __construct(array $base)
  {
    $this->base = $base;
  }

  public function dif (array $vars ) 
  {
    $result = array();
    foreach ( $this->base as $base )
    if(!in_array($base, $vars))
      $result[] = $base;
    return $result;
  }

$result = (new MyArrayOperations($allmodels))->dif($existmodels);

The class can be put in a separate file for reuse, and then just use the oneliner. And, of course the class can be extended with a kind of handy array operations.

Update

I realized that the oneliner only will work in php 5.4+, so for older versions use this:

$arrayops = new MyArrayOperations($allmodels);
$result = $arrayops->dif($existmodels);

Result in Codepad.

2 Comments

This just mess up things... not giving -1 anyway
@yes123 you are right, thanks for pointing that out. I updated and have a example here: codepad.org/A3Ut1tgl
0

adding below code to htaccess solved the problem

# Use PHP 5.3
AddType application/x-httpd-php53 .php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.