2011-11-30 35 views
2

我正面对一个非常奇怪的行为。我有$answer_styles阵列的一些数据,我需要处理:阵列奇怪地变化而不处理它

foreach($answer_styles as $id => &$answer_style){ 
    $answer_style['id'] = $id; 
    $answer_style_names[$id] = $answer_style['name']; 
} 
array_multisort($answer_style_names, SORT_ASC, SORT_STRING, $answer_styles); 

,然后我将它保存到另一个变量,以备后用:$stats['answer_styles'] = $answer_styles;

现在,我需要步入原始数组使用foreach循环。我已经做到了这一点:

debug($stats['answer_styles']); 
foreach($answer_styles as $answer_style){ 
    debug($stats['answer_styles']); 
     ... 

的问题是,第一调试显示它应该显示的数据,但第二调试显示了第一个覆盖的最后一个记录(所以,从1,2,3, 4它现在显示1,2,3,1)。为什么会发生这种情况,因为我不操纵$stats阵列,而是$answer_styles呢?

EDIT

这是用于第一,分别地,第二调试输出:

app/models/test.php (line 299) 

Array 
(
[0] => Array 
    (
     [name] => Alege din 3 
     [count] => 8 
     [correct] => 2 
     [id] => 3 
    ) 

[1] => Array 
    (
     [name] => Alege din 4 
     [count] => 3 
     [correct] => 2 
     [id] => 2 
    ) 

[2] => Array 
    (
     [name] => Alege din 6 
     [count] => 7 
     [correct] => 3 
     [id] => 4 
    ) 

[3] => Array 
    (
     [name] => Scrie raspunsul 
     [count] => 2 
     [correct] => 1 
     [id] => 1 
    ) 

) 


app/models/test.php (line 301) 

Array 
(
[0] => Array 
    (
     [name] => Alege din 3 
     [count] => 8 
     [correct] => 2 
     [id] => 3 
    ) 

[1] => Array 
    (
     [name] => Alege din 4 
     [count] => 3 
     [correct] => 2 
     [id] => 2 
    ) 

[2] => Array 
    (
     [name] => Alege din 6 
     [count] => 7 
     [correct] => 3 
     [id] => 4 
    ) 

[3] => Array 
    (
     [name] => Alege din 3 
     [count] => 8 
     [correct] => 2 
     [id] => 3 
    ) 

) 
+0

你能不能显示输出请 – Sedz

回答

6

这是因为你保持参照的数组元素与此表达& $ answer_style和使用第二个循环中的变量名称相同。

做:

unset($answer_style); 

第一个环和事以后,将其固定。

+0

哦,很尴尬,我忘了解除变量。非常感谢您的回答,它现在可行! :) – linkyndy

+0

欢迎您;) – ioseb