-2

I am trying to build an indexed array of array's in php, not a key value array of arrays, I keep getting parse error. Can you show me where my mistake is? Why is it that I can set the values of the singly dimensioned arrays but not $ax? Thanks!

<?php
class mdArray{
public $a0 = array('10','20','30','40','50','60','70','80','90');
public $a1 = array('11','21','31','41','51','61','71','81','91');
public $a2 = array('12','22','32','42','52','62','72','82','92');
public $a3 = array('13','23','33','43','53','63','73','83','93');
public $a4 = array('14','24','34','44','54','64','74','84','94');
public $a5 = array('15','25','35','45','55','65','75','85','95');
public $ax = array($a0,$a1,$a2,$a3,$a4,$a5);
}
?>
5
  • 1
    Which line has a syntax error? What is the exact error you get? Commented May 23, 2014 at 19:13
  • 1
    @RocketHazmat - I'd imagine public $ax = array($a0,$a1,$a2,$a3,$a4,$a5); which is dependent on run-time information Commented May 23, 2014 at 19:16
  • 2
    Why don't you do : class mdArray{ public $ax = array( array('10','20','30','40','50','60','70','80','90'), array('11','21','31','41','51','61','71','81','91'), array('12','22','32','42','52','62','72','82','92'), array('13','23','33','43','53','63','73','83','93'), array('14','24','34','44','54','64','74','84','94'), array('15','25','35','45','55','65','75','85','95'), ); } Commented May 23, 2014 at 19:17
  • @RocketHazmat -Parse error: parse error, expecting ')'' in - on line 9` Commented May 23, 2014 at 19:23
  • @MarkBaker I am writing a function that takes small values to create a string with implode. I add a single charter to the end of the string, I do this for every array, then I implode all the strings together into one string, I'm trying to pass only one parameter into the function Commented May 23, 2014 at 19:31

1 Answer 1

3

It's probably this line:

public $ax = array($a0,$a1,$a2,$a3,$a4,$a5);

You need to do that in your constructor:

<?php
    class mdArray{
        public $a0 = array('10','20','30','40','50','60','70','80','90');
        public $a1 = array('11','21','31','41','51','61','71','81','91');
        public $a2 = array('12','22','32','42','52','62','72','82','92');
        public $a3 = array('13','23','33','43','53','63','73','83','93');
        public $a4 = array('14','24','34','44','54','64','74','84','94');
        public $a5 = array('15','25','35','45','55','65','75','85','95');
        public $ax;

        function __construct(){
            $this->ax = array($this->a0,$this->a1,$this->a2,$this->a3,$this->a4,$this->a5);
        }
    }
?>

DOCS: http://www.php.net/manual/en/language.oop5.properties.php

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

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

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.