Jump to content

Array Calculation


asterdeve

Recommended Posts

Again I am stuck with PHP array calculation, please if anyone understands my question try to help me out, let's explain...

 

[data.txt content]

 
    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 Code Starting from here.

 
    $data = file_get_contents('./data.txt', TRUE);
    $lines = explode("\n", $data);
    
    foreach ($lines as $line) {
        if (!preg_match('/( 0.00%)/', $line)) {
         continue;
        }
        $data = explode(' ', $line);
        $list[] = $data;
    }
    
    foreach ($list as $qoscalc) {
        $average[] = ($qoscalc[17] * 1000 / 2);
        $jitter[] = (int)$qoscalc[14];
        $packet_loss[] = (int)$qoscalc[13];
    }
    print_r($average);
 

Till here the code is working fine it's giving me the output of `$average` array this >> 

 

    Array ( [0] => 10 [1] => 0 [2] => 0 )

 

After that, I couldn't do this math with the array, if I convert them in variable & I go with 1 data only then code is working fine, but when I try to get the result of all I couldn't make it, please help me if anyone understands my question.

 

 
    $effective_latency = ($average + $jitter * 2 + 10 );
    
    if ($effective_latency < 160) {
        $r_value = 93.2 - ($effective_latency / 40);
    } else {
        $r_value = 93.2 - ($effective_latency - 120) / 10;
    }
    
    $r_value = $r_value - ($packet_loss * 2.5);
    
    $mosresult = 1 + (0.035) * $r_value + (0.000007) * $r_value * ($r_value - 60) * (100 - $r_value);
    
    $moslist[] = $mosresult;
 

I want to get all 3 array result, its suppose to be like this example: Array ( [0] => 4.40372901469 [1] => 3.40372901469 [2] => 4.90372901469 )

 

$i = 0; $t = 0; $e = 0; $g = 0; $f = 0; $p = 0; $b = 0;
 
foreach ($moslist as $mos) {
$i++;
if ($mos <= "5") {
     $qosq = 'Excellent';
     $e++; 
} else if ($mos <= "4") {
     $qosq = 'Good';
     $g++; 
} else if ($mos < "3") {
     $qosq = 'Fair';
     $f++; 
} else if ($mos <= "2") {
     $qosq = 'Poor';
     $p++; 
} else if ($mos <= "1") {
     $qosq = 'Bad';
     $b++; 
} else {
continue;
}
$t++;
}
echo $qosq, "<br><br>\n";
Edited by requinix
Link to comment
Share on other sites

Not sure what your calculation is doing but I would start off like this

 

$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));
}

 

Now your $list array is much more readable and easier to handle - no guessing at the numeric keys, just access with the name keys (eg ['Peer'] )

 

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
        )

)
  • Like 1
Link to comment
Share on other sites

Thanks,

for your replay sir, I have tried your code but I am getting an error, 

 

Warning: array_combine(): Both parameters should have an equal number of elements in

 

    $list[] = array_combine($heads, explode(' ',$str));

 

this line has some issues

Link to comment
Share on other sites

Thanks,

for your replay sir, your code is working fine, but I am facing one issue that when I called the array I have to do like this way ($list[0]['Duration']); then its showing 0 array duration field but I want when I called [Duration] all field which name is Duration it will show like this somehow $list['Duration'], I hope you understand my question, Thanks ones again for helping

Link to comment
Share on other sites

You need $list[0]['Duration'] because there is an array of records, each with a Duration. You could process the list with a foreach() loop thus

foreach ($list as $rec) {
    // here you can reference $rec['Duration'];
}

Or you could

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

which would gather all the duration values into their own array eg print_r($durations) gives

Array
(
    [0] => 00:01:12
    [1] => 00:00:00
    [2] => 00:00:08
)
Link to comment
Share on other sites

Dear,

sir, the new issue is raising when it was 3 content in data.txt it was fine but when there is more data it's not working anymore, 

 

please check this data.txt content then you will understand https://down.uploadfiles.io/get/ba6fr  their include 56 line but array giving only 18 line it means only half of all lines, hope you understand me 

Link to comment
Share on other sites

That download isn't going to happen. Why don't you just paste the text contents inside code tags. Like this

 

DATA.TEXT

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

Edited by Barand
Link to comment
Share on other sites

I can't do that, because this code is just dummy code, real code will take through API of asterisk & the data will be not same everytime different data, all content will be changed but we have to take everytime correct array offset like if we use $list[12] for getting Jitter so it must be all Jitter should be in offset [12] now we are facing with that for each line offset are changing, like if 1st line jitter offset is [12] for next line its become [13] or [14] or [11], but we want the offset will remain same(static), I hope you got my issues, waiting for your helpful feedback, thanks...

 

please download this text if you can & check all content are different but we want all offset will remain same.

 

 

https://down.uploadfiles.io/get/0neur

Edited by asterdeve
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.