【问题标题】:How-to remove rows from array with missing columns如何从缺少列的数组中删除行
【发布时间】:2019-05-13 16:03:59
【问题描述】:

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

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-packets 或 rx-packets 的 dst-address 是什么。

关于信息,这个数组是动态的。我的 PHP 代码将在 while(true) 循环(无数据存储)上从设备(路由器)获取它。

【问题讨论】:

标签: php arrays filter


【解决方案1】:
$arrayVar = []; // Replace with your outter array;
$correctCount = 6;
$index = 0;
foreach($arrayVar as $subArray) {
  if(count($subArray) !== $correctCount) {
    unset($arrayVar[$index];
  }
  $index++;
}

这循环遍历数组,计算子数组具有正确数量的项目,如果没有,则删除它们。每次遍历索引都会增加 1,因此我们知道在哪里删除不正确的子数组。

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

【讨论】:

  • 这正是我想要的。它解决了我的问题。谢谢。
猜你喜欢
  • 2021-04-29
  • 2021-05-04
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
相关资源
最近更新 更多