在php中构建一个数组数组

时间:2014-05-23 19:12:47

标签: php multidimensional-array

我正在尝试在php中构建一个索引数组数组,而不是数组的键值数组,我不断得到解析错误。你能告诉我我的错误在哪里吗?为什么我可以设置单个尺寸数组的值而不是$ ax?谢谢!

<?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);
}
?>

1 个答案:

答案 0 :(得分:3)

可能就是这一行:

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

您需要在构造函数中执行此操作:

<?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

  

此声明可能包括初始化,但此初始化必须是常量值 - 也就是说,它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估。