你重置在数组中的项打破逻辑通过数据进行迭代 [英] unsetting an item in array has broken logic to iterate through data

查看:188
本文介绍了你重置在数组中的项打破逻辑通过数据进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following logic inside my controller:

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数据环路不再工作。

For some reason, after I filter out a record from the array, my logic to loop through the json data is no longer working.

下面的逻辑来遍历JSON数据:

Here's the logic to loop through the json data:

                    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数据看起来像;

The alert says "undefined". I also dumped the data before and after removing the record from the array and here's what the json data looks like;

在拆卸记录:

[09:36:52.986] [{VLANID:1,产品名称:VLAN1,状态:基于端口的声音:否,珍宝:否},{VLANID:3 ,名称:VLAN3状态:基于端口的声音:否,珍宝:否},{VLANID:8,名称:VLAN8状态:基于端口声音:否,珍宝:否},{VLANID:16,产品名称:VLAN16状态:基于端口的声音:否,珍宝:否},{VLANID: 20,产品名称:VLAN20状态:基于端口的声音:否,珍宝:否},{VLANID:33,产品名称:VLAN33状态:基于端口声音:否,珍宝:否},{VLANID:64,产品名称:VLAN64状态:基于端口的声音:否,珍宝:否},{ VLANID:65,产品名称:VLAN65状态:基于端口的声音:否,珍宝:否},{VLANID:66,产品名称:VLAN66状态:港口以诚为本,声:否,珍宝:否},{VLANID:80后,名称:VLAN80状态:基于端口的声音:否,珍宝:否} {VLANID:96,产品名称:VLAN96状态:基于端口的声音:否,珍宝:否},{VLANID:101,产品名称:vlan101上状态: 基于端口的声音:否,珍宝:否},{VLANID:128,产品名称:VLAN128状态:基于端口的声音:否,珍宝:不},{VLANID:131,产品名称:VLAN131状态:基于端口的声音:否,珍宝:否},{VLANID:417,产品名称:VLAN417现状:基于端口的声音:否,珍宝:否}]

[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"}]

AFTER:

[09:34:41.797({1:{VLANID:3,名称:VLAN3状态:基于端口的声音:否,珍宝:否},2: {VLANID:8,名称:VLAN8状态:基于端口的声音:否,珍宝:否},3:{VLANID:16,产品名称:VLAN16状态:基于端口的声音:否,珍宝:否},4:{VLANID:20,产品名称:VLAN20状态:基于端口的声音:否,珍宝:否},5:{VLANID:33,产品名称:VLAN33状态:基于端口的声音:否,珍宝:否},6:{VLANID:64 ,产品名称:VLAN64状态:基于端口的声音:否,珍宝:否},7:{VLANID:65,产品名称:VLAN65状态:基于端口 ,声音:否,珍宝:否},8:{VLANID:66,产品名称:VLAN66状态:基于端口的声音:否,珍宝:否}, 9:{VLANID:80后,名称:VLAN80状态:基于端口的声音:否,珍宝:否},10:{VLANID:96,产品名称:VLAN96 ,状态:基于端口的声音:否,珍宝:否},11:{VLANID:101,产品名称:vlan101上状态:基于端口的声音:否 ,珍宝:否},12:{VLANID:128,产品名称:VLAN128状态:基于端口的声音:否,珍宝:否},13:{VLANID 131,产品名称:VLAN131状态:基于端口的声音:否,珍宝:否},14:{VLANID:417,产品名称:VLAN417状态:端口 - 根据声音:否,珍宝:否}})

[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,而不是开始...但是,这也不能工作。
因此,例如,我尝试了以下内容:

As you can see, it does looks slightly different. I have an opening "[" before I use the unset, whereas after I have "(". I tried to change my loop control so that the variable "i" starts at 1 instead... but that didn't work either. So for example, I tried the following:

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

而不是

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

任何帮助将是AP preciated。
谢谢!

Any help would be appreciated. Thanks!

推荐答案

让我们假设你的阵列,5个项目开始。当所有的5项present,数组键是:0,1,2,3,4

Let's say your array starts with 5 items. When all 5 items are present, the array keys are: 0, 1, 2, 3, 4

json_en code 能够正确检测这是枚举数组,并将其转换为一个JSON阵列。

json_encode properly detects this as an enumerated array, and converts it to a JSON array.

不过,假设您在索引2.现在删除的项,您有:0,1,3,4, json_en code 看到一个非按键的顺序编号,现在假定它是一个关联数组,所以你会得到一个JSON对象的键0,1,3,4,在JSON / JavaScript对象不具有<​​code>长度,所以你的code不再起作用。

However, let's say you remove the item at index 2. Now, you have: 0, 1, 3, 4. json_encode sees a non-sequential numbering of keys and now assumes it's an associative array, so you get back a JSON object with the keys 0, 1, 3, 4. Objects in JSON/Javascript don't have a length, so your code no longer works.

关键是将其传递给 json_en code 之前,使用 array_values​​ ,如果你想枚举它作为一个阵列

The trick is to use array_values before passing it to json_encode, if you want to enumerate it as an array.

另外,你可以改变在JavaScript中的循环:

Alternatively, you can change your for loop in JavaScript to:

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

虽然强烈建议将其转换为一个数组通过 array_values​​

这篇关于你重置在数组中的项打破逻辑通过数据进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆