1

I've seen a few example here on stack but they don't seem to cover this scenario.

I'm attempting this;

$flight_range = array(
                    array('range' => range(1,50),  'service' => 'Long Haul'),
                    array('range' => range(51,54), 'service' => 'Short Haul'),
                    ....
                 );

but PHP won't let me. It returns;

Parse error: syntax error, unexpected '(', expecting ')' on line 02

This does not work either;

array(range(1,50), range(51,54) ...

The problem is with trying to assign a value of range().

I have 20+ sets of range values to assign.

Can anyone suggest an easy method for achieving these sorts of array values?

EDIT; haike00, Jack and Sean are right.

Maybe my question should be how do i make $flight_range a member variable of a class;

private $flight_range = array(array('range' => range(1,50), 'service' => 'Long Haul'));
4
  • 1
    Is this array declared at the very top of your script? It does not look like this is where the error lies. Try making only this array in a new script and do a print_r again. Commented May 6, 2013 at 3:41
  • 2
    There's no problem with your code. Commented May 6, 2013 at 3:44
  • 1
    There must be another issue in your script. Your posted code works - see this phpfiddle - phpfiddle.org/main/code/sfb-6v6 Commented May 6, 2013 at 3:50
  • go to the line where the error occurs according to parsing error.. then copy/paste that line here. Those .... does not help much. Commented May 6, 2013 at 4:07

2 Answers 2

1

What is the problem with doing this in your constructor?

class MyClass {
        private $flight_range;

        public function __construct() {
                $this->flight_range = array(
                        array(
                                'range' => range( 1, 50 ),
                                'service' => 'Long Haul'
                        )
                );
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nothing. Thanks Jacob. I was assigning values to the members in their declaration. class my_class { private $flight_range = 'blah blah'; public function __construct() { ... do some contruct stuff here ... } } Seems that doesn't work in this case. Thank you all.
0

Its working fine on my end.

$flight_range = array(
               array('range' => range(1,50) ,  'service' => 'Long Haul'),                 array('range' => range(51,54),'service' => 'Short Haul'));

 print_r($flight_range);    

Just copy and paste above code and run it.

1 Comment

This should be a comment. And there are at least two comments that say the same thing.

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.