如何从缺少列的数组中删除行

时间:2019-05-13 16:03:59

标签: php arrays filter

好...所以,我有一个系统,可以为我提供一系列物品。这些项目显示如下:

Array
(
    [1] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.1
            [tx] => 0
            [rx] => 0
            [tx-packets] => 0
            [rx-packets] => 0
        )

    [2] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.112
            [tx] => 0
            [rx] => 0
            [tx-packets] => 0
            [rx-packets] => 0
        )

    [3] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.130
            [tx] => 0
            [rx] => 0
            [tx-packets] => 0
            [rx-packets] => 0
        )

    [4] => Array
        (
            [tx] => 0
            [rx] => 0
            [tx-packets] => 0
            [rx-packets] => 0
        )

    [5] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.5
            [tx] => 0
            [rx] => 496
            [tx-packets] => 0
            [rx-packets] => 1
        )

    [6] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.6
            [tx] => 0
            [rx] => 528
            [tx-packets] => 0
            [rx-packets] => 1
        )

    [7] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.16
            [tx] => 624
            [rx] => 0
            [tx-packets] => 1
            [rx-packets] => 0
        )

    [8] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.24
            [tx] => 0
            [rx] => 448
            [tx-packets] => 0
            [rx-packets] => 1
        )

    [9] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.31
            [tx] => 0
            [rx] => 528
            [tx-packets] => 0
            [rx-packets] => 1
        )

    [10] => Array
        (
            [protocol] => ip
            [dst-address] => 10.0.0.44
            [tx] => 10592
            [rx] => 13200
            [tx-packets] => 20
            [rx-packets] => 28
        )

您已经看到,条目ID 4缺少表中的某些列。

在PHP中,如何从数组中删除错误的行? 我想正确修复数组,以便对其重新排序以显示类似以下内容: -顶级TX -顶级RX -顶级TX包 -顶级RX数据包 -具有最多tx或rx或tx数据包或rx数据包的dst地址是什么。

有关信息,此数组为动态数组。我的PHP代码会在while(true)循环(没有数据存储)上从设备(路由器)中获取它。

1 个答案:

答案 0 :(得分:0)

$arrayVar = []; // Replace with your outter array;
$correctCount = 6;
$index = 0;
foreach($arrayVar as $subArray) {
  if(count($subArray) !== $correctCount) {
    unset($arrayVar[$index];
  }
  $index++;
}

这将遍历整个数组,计算子数组具有正确数量的项目,如果没有,则将其删除。索引在每次通过时都会递增1,因此我们知道在哪里删除不适当的子数组。

您可以将计数更改为正确的项目数。