你重置在数组中的项具有破逻辑通过数据进行迭代(unsetting an item in array

2019-08-08 03:32发布

我有我的控制器内以下逻辑:

public function showvlans()
{
    $vlans=$this->switches_model->show_known_vlans($this->uri->segment(5), $this->uri->segment(4));
    //filter out VLAN 1 if its included in the list. 
    $key = array_search('1', $vlans);
    unset($vlans[$key]);
    header ('Content-Type: application/json; charset=UTF-8');
    echo json_encode($vlans);  

} // end showvlans  

出于某种原因,我从阵列中筛选出一个记录之后,通过JSON数据我的逻辑循环不再工作。

这里的逻辑通过JSON数据循环:

alert(returnDataFromController.length);
//loop through results
for(i = 0; i < returnDataFromController.length; i++) {
    alert(returnDataFromController[i].VlanId);
    htmlstring = htmlstring +  "<tr><td><a href=>"+returnDataFromController[i].VlanId+"</a></td><td>"+ returnDataFromController[i].Name+"</td></tr>";                         
}

线索:

警报说,“不确定”。
我也甩数据前,从数组删除记录后这里就是JSON数据是这样的:

在卸下记录:

[09:36:52.986] [
    {VlanId:"1", Name:"VLAN1", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"3", Name:"VLAN3", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"8", Name:"VLAN8", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"16", Name:"VLAN16", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"20", Name:"VLAN20", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"33", Name:"VLAN33", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"64", Name:"VLAN64", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"65", Name:"VLAN65", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"66", Name:"VLAN66", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"80", Name:"VLAN80", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"96", Name:"VLAN96", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"101", Name:"VLAN101", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"128", Name:"VLAN128", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"131", Name:"VLAN131", Status:"Port-based", Voice:"No", Jumbo:"No"},
    {VlanId:"417", Name:"VLAN417", Status:"Port-based", Voice:"No", Jumbo:"No"}]

后:

[09:34:41.797] (
    {1:{VlanId:"3", Name:"VLAN3", Status:"Port-based", Voice:"No", Jumbo:"No"},
     2:{VlanId:"8", Name:"VLAN8", Status:"Port-based", Voice:"No", Jumbo:"No"},
     3:{VlanId:"16", Name:"VLAN16", Status:"Port-based", Voice:"No", Jumbo:"No"},
     4:{VlanId:"20", Name:"VLAN20", Status:"Port-based", Voice:"No", Jumbo:"No"},
     5:{VlanId:"33", Name:"VLAN33", Status:"Port-based", Voice:"No", Jumbo:"No"},
     6:{VlanId:"64", Name:"VLAN64", Status:"Port-based", Voice:"No", Jumbo:"No"},
     7:{VlanId:"65", Name:"VLAN65", Status:"Port-based", Voice:"No", Jumbo:"No"},
     8:{VlanId:"66", Name:"VLAN66", Status:"Port-based", Voice:"No", Jumbo:"No"},
     9:{VlanId:"80", Name:"VLAN80", Status:"Port-based", Voice:"No", Jumbo:"No"},
     10:{VlanId:"96", Name:"VLAN96", Status:"Port-based", Voice:"No", Jumbo:"No"},
     11:{VlanId:"101", Name:"VLAN101", Status:"Port-based", Voice:"No", Jumbo:"No"},
     12:{VlanId:"128", Name:"VLAN128", Status:"Port-based", Voice:"No", Jumbo:"No"},
     13:{VlanId:"131", Name:"VLAN131", Status:"Port-based", Voice:"No", Jumbo:"No"},
     14:{VlanId:"417", Name:"VLAN417", Status:"Port-based", Voice:"No", Jumbo:"No"}})

正如你所看到的,它看起来略有不同。 我有一开口[之前我用的是未设置的,而我后(我试图改变我的闭环控制,使得可变i开始于1 ... ...但是这也不能工作。

因此,例如,我试过如下:

for(i = 1; i < returnDataFromController.length; i++) {

代替

for(i = 0; i < returnDataFromController.length; i++) {

Answer 1:

比方说,你的阵列,5个项目开始。 当所有5个项目都存在,该数组键是:0,1,2,3,4

json_encode正确检测到该作为枚举阵列,并将其转换为一个JSON阵列。

但是,假设你在索引2.现在删除的项,您有:0,1,3,4 json_encode看到按键的非顺序编号,现在假定它是一个关联数组,所以你得到一个JSON对象与键0,1,3,4,在JSON / JavaScript对象没有一个length ,因此您的代码将不再有效。

诀窍是使用array_values传递到前json_encode ,如果要列举它作为一个数组。

或者,你可以改变你for JavaScript中循环:

for (var i in returnDataFromController) {
    if (returnDataFromController.hasOwnProperty(i)) {
        /* your code here */
    }
}

虽然强烈建议将其转换为经由阵列array_values



Answer 2:

的JavaScript / JSON 对象数组之间进行区分。 对象是使用键-值对{ }语法,阵列是数值使用索引列表[ ]

json_encode荷兰国际集团一个PHP数组,只能用连续数字索引阵列将被编码为JSON / JavaScript 数组 ,否则他们会成为对象 。 例如:

array('foo', 'bar', 'baz')    -> ["foo", "bar", "baz"]
array(0 => 'foo', 2 => 'bar') -> {"0":"foo", "2":"bar"} 

为了确保您的阵列具有连续的数字指标,用array_values



文章来源: unsetting an item in array has broken logic to iterate through data