奇怪的数组重复行为

时间:2012-10-13 11:46:01

标签: php arrays dynamic-arrays

嗨,这是我关于stackoverflow的第一个问题。

我正在为应用程序编写一个表构建类。

首先使用以下代码配置表格。

$table->setPage('page.rows','20');
$table->setPage('page.start','0');

$table->setTable('table.name','customertable');
$table->setTable('table.higlight','true');

$table->setColumn('column.name','id');                          
$table->setColumn('column.key','index');                        
$table->setColumn('column.heading','none');                     
$table->setColumn('column.issort','false');                     
$table->setColumn('column.islink','false');                     
$table->setColumn('column.ischeck','true');                     
$table->insertColumn();                                         

$table->setColumn('column.name','jobref');                                          
$table->setColumn('column.key','job-ref');                      
$table->setColumn('column.heading','RB Ref');                       
$table->setColumn('column.issort','true');                      
$table->setColumn('column.islink','true');                      
$table->setColumn('column.ischeck','false');                
$table->insertColumn();

$table->setColumn('column.name','type');                                            
$table->setColumn('column.key','type');                     
$table->setColumn('column.heading','Job Type');                     
$table->setColumn('column.issort','true');                      
$table->setColumn('column.islink','true');                      
$table->setColumn('column.ischeck','false');                
$table->insertColumn(); 

除了setColumn方法之外,值与通过下面的函数调用将值添加到临时数组并不重要。

public function setColumn($key,$value){
    $this->colParamSet[$key] = $value;      
    }

然后当我完成我的参数列表时,我使用

$table->insertColumn(); 

调用此代码

public function insertColumn(){

    $this->columnConfig[$this->colParamSet['column.name']] = $this->colParamSet;        

    print_r($this->columnConfig); #this is for test purposes not part of the final code

    }

我希望到目前为止我已经解释了所有问题现在....这里是输出

Array ( 

[id] => Array ( [column.name] => id [column.key] => index [column.heading] => none [column.issort] => false [column.islink] => false [column.ischeck] => true ) 

) 

Array ( 

[id] => Array ( [column.name] => id [column.key] => index [column.heading] => none     [column.issort] => false [column.islink] => false [column.ischeck] => true ) 
[jobref] => Array ( [column.name] => jobref [column.key] => job-ref [column.heading] => RB Ref [column.issort] => true [column.islink] => true [column.ischeck] => false ) 

) 

Array ( 

[id] => Array ( [column.name] => id [column.key] => index [column.heading] => none [column.issort] => false [column.islink] => false [column.ischeck] => true ) 
[jobref] => Array ( [column.name] => jobref [column.key] => job-ref [column.heading] => RB Ref [column.issort] => true [column.islink] => true [column.ischeck] => false ) 
[type] => Array ( [column.name] => type [column.key] => type [column.heading] => Job Type [column.issort] => true [column.islink] => true [column.ischeck] => false ) 

)

每个配置集是表中每列的设置,指向数组的键是db coloumn名称,用于从包含表数据的assoc数组中提取数据。

我不明白为什么我会重复数组。我有这个工作的唯一方法是使用下面的代码

public function insertColumn(){
    $this->columnConfig = array();
    $this->columnConfig[$this->colParamSet['column.name']] = $this->colParamSet;        
    $this->colParamSet = array();


    print_r($this->columnConfig);

    }

这工作正常并输出我需要的但我不能理解为什么它的确有效,因为它似乎我完全清除了数组然后插入一个带有数组的单元格然后再次清除它但它不是表现那样,因为我不理解它,我不能依赖它,因为我可能已经创造了一个不稳定的黑客,它会在生产中产生摇摆。

我希望我能让这个问题变得可以理解。

提前致谢。

抱歉,我忘了显示我试图实现的输出,所以这里是

Array ( 

[id] => Array ( [column.name] => id [column.key] => index [column.heading] => none         [column.issort] => false [column.islink] => false [column.ischeck] => true ) 
[jobref] => Array ( [column.name] => jobref [column.key] => job-ref [column.heading] =>     RB Ref [column.issort] => true [column.islink] => true [column.ischeck] => false ) 
[type] => Array ( [column.name] => type [column.key] => type [column.heading] => Job Type [column.issort] => true [column.islink] => true [column.ischeck] => false ) 

)

比较代码

我希望我不会因为这个问题太大而遇到麻烦但是我已经生成了以下代码进行比较

$temp = array(); 
$final = array(); 

function setconfig($key,$value){ 
    global $temp;     
    $temp[$key] = $value; 
    } 

function insertarray(){ 
    global $final, $temp; 
    $final[$temp['config.name']] = $temp; 
    } 

setconfig('config.name','bob'); 
setconfig('config.width','50'); 
setconfig('config.height','50'); 
setconfig('config.class','bobs-box'); 
insertarray(); 

setconfig('config.name','jon'); 
setconfig('config.width','150'); 
setconfig('config.height','150'); 
setconfig('config.class','jons-box'); 
insertarray(); 

setconfig('config.name','sue'); 
setconfig('config.width','150'); 
setconfig('config.height','150'); 
setconfig('config.class','sues-box'); 
insertarray(); 

print_r($final); 

输出是

下面的所需输出
Array ( 

[bob] => Array ( [config.name] => bob [config.width] => 50 [config.height] => 50 [config.class] => bobs-box ) 
[jon] => Array ( [config.name] => jon [config.width] => 150 [config.height] => 150 [config.class] => jons-box ) 
[sue] => Array ( [config.name] => sue [config.width] => 150 [config.height] => 150 [config.class] => sues-box ) 
)

所以有人能告诉我在一个对象内的数组与为什么它的行为方式不同的区别?或者我应该忘记它并使用修复并继续前进。

2 个答案:

答案 0 :(得分:1)

您有一个对象$table

$this->columnConfig是该对象的属性。

每次调用insertColumn()方法时,它都使用之前使用过的columnConfig

当您添加第$this->columnConfig = array();行时,您可以“重置”该属性。

希望你理解。

答案 1 :(得分:0)

抱歉,每个人都回答了我的问题。当便士下降时,我意识到我曾经是多么麻木。

我在数组的构建迭代期间打印出数组,因此值似乎重复了但不是。 DOH !!当孩子们回家时,应该学会在周末独自离开。介绍我自己的好方法我把HAHA