阵列爆炸问题

时间:2018-04-09 15:00:45

标签: php

所有表明我面临阵列爆炸的问题

data.txt内容低于

Peer             Call ID      Duration Recv: Pack  Lost       (     %) Jitter Send: Pack  Lost       (     %) Jitter
139.59.232.196   0bb9262d6a1  00:01:12 0000003558  0000000000 ( 0.00%) 0.0000 0000001177  0000000000 ( 0.00%) 0.0200
139.59.232.196   41283499492  00:00:00 0000000000  0000000000 ( 0.00%) 0.0000 0000000000  0000000000 ( 0.00%) 0.0000
139.59.232.196   7033a541240  00:00:08 0000000000  0000000000 ( 0.00%) 0.0000 0000000019  0000000000 ( 0.00%) 0.0000
3 active SIP channels

PHP代码低于

$heads = [ 'Peer', 0, 0, 'CallId', 0, 'Duration', 'Recv', 0, 'RecvLost', 0, 'RecvPC', 'RJitter', 'Send', 0, 'SendLost', 0, 'SendPC', 'SJitter'];

$data = file('data.txt', FILE_IGNORE_NEW_LINES);
unset($data[0]); // Lose heading line

$list = [];
foreach ($data as $str) {
    $list[] = array_combine($heads, explode(' ',$str));
}

的print_r($列表);在这样的事情之下。

Array
(
    [0] => Array
        (
            [Peer] => 139.59.232.196
            [0] => (
            [CallId] => 0bb9262d6a1
            [Duration] => 00:01:12
            [Recv] => 0000003558
            [RecvLost] => 0000000000
            [RecvPC] => 0.00%)
            [RJitter] => 0.0000
            [Send] => 0000001177
            [SendLost] => 0000000000
            [SendPC] => 0.00%)
            [SJitter] => 0.0200
        )

    [1] => Array
        (
            [Peer] => 139.59.232.196
            [0] => (
            [CallId] => 41283499492
            [Duration] => 00:00:00
            [Recv] => 0000000000
            [RecvLost] => 0000000000
            [RecvPC] => 0.00%)
            [RJitter] => 0.0000
            [Send] => 0000000000
            [SendLost] => 0000000000
            [SendPC] => 0.00%)
            [SJitter] => 0.0000
        )

    [2] => Array
        (
            [Peer] => 139.59.232.196
            [0] => (
            [CallId] => 7033a541240
            [Duration] => 00:00:08
            [Recv] => 0000000000
            [RecvLost] => 0000000000
            [RecvPC] => 0.00%)
            [RJitter] => 0.0000
            [Send] => 0000000019
            [SendLost] => 0000000000
            [SendPC] => 0.00%)
            [SJitter] => 0.0000
        )

)

当我打电话给他们时,我必须这样做,$ list [' 0'] [持续时间]然后它给出了数组0持续时间字段数据,但我希望当我这样打电话时$ list [持续时间]它将为我提供所有数组持续时间数据,如下所示

00:01:12 00:00:00 00:00:08

感谢您的关注......

1 个答案:

答案 0 :(得分:2)

您需要array_column功能:

$durations = array_column($list, 'Duration');

对于php 较旧而不是5.5,您需要一个简单的foreach