PHP foreach及其参考

PHP foreach及其参考,php,arrays,reference,foreach,Php,Arrays,Reference,Foreach,我试图在PHP中使用嵌套的foreach循环中的指针修改值。。 但是,以下行似乎不起作用: // Assign a the attribs value to the array $link_row['value'] = $args[ $u_value ]; 变量$args[$u_值];已填充,可以无问题输出,但当我将其添加到$link\u行引用时,它似乎没有设置 foreach ($unique_links as $link_id => &$link_attr) {

我试图在PHP中使用嵌套的foreach循环中的指针修改值。。 但是,以下行似乎不起作用:

// Assign a the attribs value to the array
$link_row['value'] = $args[ $u_value ];
变量$args[$u_值];已填充,可以无问题输出,但当我将其添加到$link\u行引用时,它似乎没有设置

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

我认为您可能正在覆盖循环中的值。为了测试这一点,你可以这样做

$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";
echo“链接行值:”。打印($Link\u Row,true)。“
\n”;

这将显示所有被强制转换/设置为$link_row['value']的值,如果您看到多个索引,这些值将被覆盖

,因为我看不到您的数组,我不知道哪些是整数,哪些关联,等等

就我所知,没有理由引用$u_值。这不会造成任何伤害,但两种方式都没有区别

更重要的是,当你的第二个if条件为真时,在你到达它之前,你会有一个错误

$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 "&nbsp&nbsp&nbsp&nbsp $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,因为它似乎杀死了整个数组,而不仅仅是你想要的部分。我猜这是由于杀死了当前正在迭代的部分而导致的一些奇怪行为。

foreach
中使用变量后,必须始终取消设置变量。即使您有两个
foreach(..)
语句,一个接一个。即使您在另一个
foreach(…)
内部有一个
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;
}

在第一个
foreach(…)
之后,有人没有
取消设置($line)
,这一事实确实弄乱了数组中的数据,因为
和$line
是一个引用,而第二个
foreach(…)
在循环数据时为其分配了不同的值,并不断覆盖最后一行数据。

我认为这不是问题。为了写入两次$link\u row['value'],必须在同一数组中有两个名为'attribute\u name'的键。可能还没有给出可接受的答案?感谢您花时间查看。。。问题似乎在于unset本身对其进行了更多的调查。。unset似乎扼杀了我实际添加的值。为了澄清(不一定是OP;也为了读者):PHP在将foreach作为引用传递时不会复制这些值。因此,对&$值所做的任何更改都会影响原始数组的值。通过使用“unset($unique_links[$link_id])”,创建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;
}