php - 在数组中添加值

时间:2012-08-17 17:34:27

标签: php arrays

我正在尝试遍历多维数组,并添加一个新的子数组。我的代码不会返回任何错误,但它也不会添加新项目。

我有以下代码:

foreach ($data['switches'] as $switch) {
    foreach ($switch['atags'] as $attributelist)  {
        $nohardwareAttribFound = false;

        foreach ($attributelist as $attribute) {
            $pos =  strpos(trim($attribute),'$attr_2_');

            if ($pos !==false)   {      

                //echo 'in the loop';
                //found it.  extract and exit loop
                $modelnumber = substr(trim($attribute),8);
                $hardwaremodel = array();
                $hardwaremodel['tag'] = 'hardware_model:'.$modelnumber;
                array_push($switch['atags'],$hardwaremodel);
                print_r($switch);
                //echo '<br>=====<br>';
                $nohardwareAttribFound = true;
            }   

        }//end foreach ($attributelist
    }// end foreach ($switch['atags']

    if ($nohardwareAttribFound==false) {
        $hardwaremodel['tag'] = 'Unknown';
        array_push($switch['atags'],$hardwaremodel);
    }//end if


}// end foreach ($data['switches']

我希望数据看起来像:

[atags] => Array ( 
  [0] => Array ( [tag] => $id_365 ) 
  [1] => Array ( [tag] => $typeid_8 ) 
  [2] => Array ( [tag] => $any_object ) 
  [3] => Array ( [tag] => $casd ) 
  [4] => Array ( [tag] => $unmounted ) 
  [5] => Array ( [tag] => $no_asset_tag ) 
  [6] => Array ( [tag] => $attr_2_1086 ) 
  [7] => Array ( [tag] => $untagged ) 
  [8] => Array ( [tag] => hardware_model:1086 ) ) ) 

最后一个数组 - 元素[8]代表我添加的新子数组。 print_r()语句看起来是正确的,但是当我遍历传递给我视图的结果时,我可以看到实际上还没有添加新的标记数组。

我需要某种替代而不是array_push()吗?

如果在循环中修改数组不是一个好主意,我可以简单地检查项目是否存在。 我如何检查每个开关的['atags']数组是否包含一个值为“$ attr_2_NNNN”的[tag],其中N是一个数字?例如,检查上面的示例数组中的元素6。挑战在于它并不总是元素6,并且您并不总是保证标签具有attr_2值。 我知道有一个in_array()函数......我会尝试类似的东西:

if (in_array(array('$attr_2_'), $switches['atags']))

我有一个关于$ nohardwareAttribFound变量的逻辑错误,我将修复它。
感谢

3 个答案:

答案 0 :(得分:1)

首先:建议您在迭代时更改数组或集合。

如果你真的想这样做,那么你应该通过引用而不是值来$switch。否则,您可以将$switch更改为您想要的任何内容,它不会反映在$data['switches']数组中。

要通过引用获取$switch,只需添加&

foreach ($data['switches'] as &$switch) { 
}

查看foreach in PHP manual

编辑在研究您的代码后,我认为这就是您所寻找的:

foreach ($data['switches'] as &$switch) { 
    $hardwaremodel = array();
    $hardwaremodel['tag'] = NULL; // initialize to NULL so we can check at the end of the loop if we have found a hardwaremodel or not (so we don't need that bool)

    foreach ($switch['atags'] as $attributelist) { 
        foreach ($attributelist as $attribute) { 
            $pos = strpos(trim($attribute), '$attr_2_'); 
            if ($pos !== false) {
                $modelnumber = substr(trim($attribute), 8);
                $hardwaremodel['tag'] = 'hardware_model:' . $modelnumber;
                break;
            }
        }
        if ($hardwaremodel['tag'] !== NULL)
            break; // exit the loop because we already found a tag
    }

    if ($hardwaremodel['tag'] === NULL)
        $hardwaremodel['tag'] = 'hardware_model:unknown';

    // Note that this is a safe place to modify the $switch array
    // as we are not currently iterating it
    array_push($switch['atags'], $hardwaremodel); 
}

答案 1 :(得分:0)

每次进入循环时,您的interation变量(即$switch)都是副本 - 您不会修改原始数组$data。为此,您需要修改完整路径,如:

array_push($data['switches']['atags'], $newVal)

或者您可以在输入循环时通过引用传递:

foreach ($data['switches'] as &$switch) 
{
   // ... 
}

答案 2 :(得分:0)

我认为你应该替换

foreach ($data['switches'] as $switch) 

foreach ($data['switches'] as &$switch) 

使用引用应该这样做。

注意:之后使用unset($ switch)来销毁引用。