取消设置数组中的项目已经破坏了迭代数据的逻辑

时间:2013-01-24 14:57:20

标签: php arrays json codeigniter

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

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

AFTER:

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

正如您所看到的,它看起来略有不同。在我使用unset之前我有一个开头[,而在我(之后。 我尝试更改循环控件,以便变量i1开始,而不是......但这也不起作用。

例如,我尝试了以下内容:

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

而不是

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

2 个答案:

答案 0 :(得分:1)

假设您的阵列以5个项目开头。当所有5个项目都存在时,数组键为:0,1,2,3,4

json_encode将其正确检测为枚举数组,并将其转换为JSON数组。

但是,假设你删除索引2处的项目。现在,你有:0,1,3,4。json_encode看到一个非连续的键编号,现在假设它是一个关联数组,所以你得到一个带有键0,1,3,4的JSON对象.JSON / Javascript中的对象没有length,所以你的代码不再有效。

诀窍是在将array_values传递给json_encode之前使用for,如果你想将它枚举为数组。

或者,您可以将JavaScript中的for (var i in returnDataFromController) { if (returnDataFromController.hasOwnProperty(i)) { /* your code here */ } } 循环更改为:

array_values

虽然强烈建议通过{{1}}

将其转换为数组

答案 1 :(得分:1)

Javascript / JSON区分对象数组。对象是使用{ }语法的键值对,数组是使用[ ]的数字索引列表。

json_encode PHP数组时,只有连续数字索引的数组才会被编码为JSON / Javascript 数组,否则它们将成为物体。 E.g:

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

要确保您的数组具有连续的数字索引,请使用array_values