我试图在PHP中使用嵌套的foreach循环中的指针修改值。 但是,以下行似乎不起作用:
// Assign a the attribs value to the array
$link_row['value'] = $args[ $u_value ];
变量$ args [$ u_value];填充,可以输出没有问题,但当我将它添加到$ link_row引用它只是似乎没有设置..
foreach ($unique_links as $link_id => &$link_attr)
{
foreach($link_attr as &$link_row)
{
foreach($link_row as $u_attr => &$u_value)
{
if ($u_attr == 'attribute_name')
{
// Assign a the attribs value to the array
$link_row['value'] = $args[ $u_value ];
// If one of the values for the unique key is blank,
// we can remove the entire
// set from being checked
if ( !isset($args[ $u_value ]) )
{
unset($unique_links[$link_id] );
}
}
}
}
}
答案 0 :(得分:4)
在foreach
中使用后,必须始终取消设置变量。即使你有两个foreach(..)
语句,一个接一个。即使您在另一个foreach(..)
内有foreach(..)
。 没有例外!
foreach ($unique_links as $link_id => &$link_attr)
{
foreach($link_attr as &$link_row)
{
foreach($link_row as $u_attr => &$u_value)
{
if ($u_attr == 'attribute_name')
{
// Assign a the attribs value to the array
$link_row['value'] = $args[ $u_value ];
// If one of the values for the unique key is blank,
// we can remove the entire
// set from being checked
if ( !isset($args[ $u_value ]) )
{
unset($unique_links[$link_id] );
}
}
}
unset($u_value); // <- this is important
}
unset($link_row); // <- so is this
}
unset($lnk_attr); // <- and so is this, even if you reached the end of your program or the end of a function or a method and even if your foreach is so deeply indented or on such a long line that you're not sure what code might follow it, because another developer (maybe even you) will come back and read the code and he might not see that you used a reference in a foreach
这是另一段有趣的代码,不久前搞砸了一个大项目:
foreach ($data as $id => &$line) {
echo "This is line {$id}: '{$line}'\n";
$line .= "\n";
}
echo "And here is the output, one line of data per line of screen:\n";
foreach ($data as $id => &$line) {
echo $line;
}
有人在第一个unset($line)
之后没有foreach(..)
的事实确实搞砸了数组中的数据,因为&$line
是引用而第二个foreach(..)
在循环访问数据时为其分配了不同的值,并且它会覆盖最后一行数据。
答案 1 :(得分:0)
我认为你可能会覆盖循环中的值。要测试这个你可以做这样的事情
$link_row['value'] = $args[ $u_value ];
将此更改为
$link_row[] = $args[ $u_value ];
然后在循环之外添加此
echo "Link Row Value(s):<pre>".print_r($link_row,true)."</pre><br />\n";
这将显示正在转换/设置为$ link_row ['value']的所有值,如果您看到多个索引覆盖了值
答案 2 :(得分:0)
由于我看不到你的数组,我不知道哪个是整数,哪个是关联等等。
到目前为止,我看到没有理由引用$ u_value。它没有任何伤害,但无论如何都没有任何区别。
更重要的是,只要你的第二个if条件成立,你就会在到达行之前出现错误
$link_row['value'] = $args[ $u_value ];
也许你想用
$link_row['value'] = isset($args[$u_value]) ? $args[ $u_value ] : "NOT PRESENT";
你提到的那条线似乎工作正常。
我的代码:
$args = array(100,200,300,400,500);
$unique_links = array (array( 'a' => array('attribute_name' => 1,'x' => 2, 'y' => 3, 'z' =>4),
'b' => array('attribute_name' => 3,'x' => 2, 'y' => 3, 'z' =>4),
'c' => array('attribute_name' => 0,'x' => 2, 'y' => 3, 'z' =>4),
'd' => array('attribute_name' => 7,'x' => 2, 'y' => 3, 'z' =>4),
'e' => array('attribute_name' => 1,'x' => 2, 'y' => 3, 'z' =>4)
));
echo_r($unique_links);
foreach ($unique_links as $link_id => &$link_attr)
{
foreach($link_attr as &$link_row)
{
foreach($link_row as $u_attr => $u_value)
{
echo "     $u_attr is $u_value <br />";
if ($u_attr == 'attribute_name')
{
// Assign a the attribs value to the array
$link_row['value'] = isset($args[$u_value]) ? $args[ $u_value ] : "NOT PRESENT";
// If one of the values for the unique key is blank, we can remove the entire
// set from being checked
if ( !isset($args[ $u_value ]) )
{
//echo "want to kill: $link_id <br />";
//unset($unique_links[$link_id] );
}
}
}
echo "<br />";
}
}
echo_r($unique_links);
我的输出:
Array
(
[0] => Array
(
[a] => Array
(
[attribute_name] => 1
[x] => 2
[y] => 3
[z] => 4
)
[b] => Array
(
[attribute_name] => 3
[x] => 2
[y] => 3
[z] => 4
)
[c] => Array
(
[attribute_name] => 0
[x] => 2
[y] => 3
[z] => 4
)
[d] => Array
(
[attribute_name] => 7
[x] => 2
[y] => 3
[z] => 4
)
[e] => Array
(
[attribute_name] => 1
[x] => 2
[y] => 3
[z] => 4
)
)
)
attribute_name is 1
x is 2
y is 3
z is 4
value is 200
attribute_name is 3
x is 2
y is 3
z is 4
value is 400
attribute_name is 0
x is 2
y is 3
z is 4
value is 100
attribute_name is 7
x is 2
y is 3
z is 4
value is NOT PRESENT
attribute_name is 1
x is 2
y is 3
z is 4
value is 200
Array
(
[0] => Array
(
[a] => Array
(
[attribute_name] => 1
[x] => 2
[y] => 3
[z] => 4
[value] => 200
)
[b] => Array
(
[attribute_name] => 3
[x] => 2
[y] => 3
[z] => 4
[value] => 400
)
[c] => Array
(
[attribute_name] => 0
[x] => 2
[y] => 3
[z] => 4
[value] => 100
)
[d] => Array
(
[attribute_name] => 7
[x] => 2
[y] => 3
[z] => 4
[value] => NOT PRESENT
)
[e] => Array
(
[attribute_name] => 1
[x] => 2
[y] => 3
[z] => 4
[value] => 200
)
)
)
我注释掉了unnset,因为它似乎会杀死整个数组,而不仅仅是你想要的部分。我猜它是一些奇怪的行为,因为杀死了你正在迭代的部分。