Jump to content

find duplicate value in text file first column and merge line value to it.


pramodh

Recommended Posts

call   number                     date                      duration
19738424064    2012-05-01 09:04:17    0.008
19738424064    2012-05-01 12:01:52    0.072
19738424064    2012-05-01 14:27:30    0.004
19738424064    2012-05-01 14:28:02    0.004
19738424065    2012-05-01 14:14:17    0.02
19738424065    2012-05-01 14:28:24    0.008
19738424064    2012-05-02 09:20:19    0.004
19738424064    2012-05-02 09:08:04    0.048
19738424065    2012-05-02 09:21:33    0.072
19738424064    2012-05-02 14:16:54    0.028
 

how do I get json php and call number should not repeated and google api line graph to calculate the total call  in hour

05-2012.txt

Link to comment
Share on other sites

You'll want to use file() to read the lines into an array. You'd then loop over each line, exploding on the tab character ("\t") to get each column (call number, dateStart, dateEnd and duration).

 

You'd want to create an array, using the call number as the key. You'd assign the call date(s) and duration together into an array. That way all information for that call number is grouped together. Example code

$lines = file('05-2012.txt', FILE_IGNORE_NEW_LINES);

$callDataArray = array();

foreach($lines as $line)
{
        // get each column, exploding on the tab character
	list($callNumber, , $callStartDate, $callEndDate, , $callDuration) = explode("\t", $line);

        // create a new array for each call number
	if(!isset($callDataArray[ $callNumber ]))
		$callDataArray[ $callNumber ] = array();

        // group call number data together
	$callDataArray[ $callNumber ][] = array($callStartDate, $callEndDate, $callDuration);
}

Will produce the following array

Array
(
    [19738424064] => Array
        (
            [0] => Array
                (
                    [0] => 2012-05-01 09:04:17
                    [1] => 2012-05-01 09:05:44
                    [2] => 0.008
                )

            [1] => Array
                (
                    [0] => 2012-05-01 12:01:52
                    [1] => 2012-05-01 12:19:17
                    [2] => 0.072
                )

            [2] => Array
                (
                    [0] => 2012-05-01 14:27:30
                    [1] => 2012-05-01 14:27:45
                    [2] => 0.004
                )

            [3] => Array
                (
                    [0] => 2012-05-01 14:28:02
                    [1] => 2012-05-01 14:28:12
                    [2] => 0.004
                )

            [4] => Array
                (
                    [0] => 2012-05-02 09:20:19
                    [1] => 2012-05-02 09:21:19
                    [2] => 0.004
                )

            [5] => Array
                (
                    [0] => 2012-05-02 09:08:04
                    [1] => 2012-05-02 09:19:46
                    [2] => 0.048
                )

            [6] => Array
                (
                    [0] => 2012-05-02 14:16:54
                    [1] => 2012-05-02 14:23:49
                    [2] => 0.028
                )

        )

    [19738424065] => Array
        (
            [0] => Array
                (
                    [0] => 2012-05-01 14:14:17
                    [1] => 2012-05-01 14:18:49
                    [2] => 0.02
                )

            [1] => Array
                (
                    [0] => 2012-05-01 14:28:24
                    [1] => 2012-05-01 14:29:58
                    [2] => 0.008
                )

            [2] => Array
                (
                    [0] => 2012-05-02 09:21:33
                    [1] => 2012-05-02 09:39:27
                    [2] => 0.072
                )

        )

)

Passing $callDataArray to json_encode will produce

{"19738424064":[["2012-05-01 09:04:17","2012-05-01 09:05:44","0.008"],["2012-05-01 12:01:52","2012-05-01 12:19:17","0.072"],["2012-05-01 14:27:30","2012-05-01 14:27:45","0.004"],["2012-05-01 14:28:02","2012-05-01 14:28:12","0.004"],["2012-05-02 09:20:19","2012-05-02 09:21:19","0.004"],["2012-05-02 09:08:04","2012-05-02 09:19:46","0.048"],["2012-05-02 14:16:54","2012-05-02 14:23:49","0.028"]],"19738424065":[["2012-05-01 14:14:17","2012-05-01 14:18:49","0.02"],["2012-05-01 14:28:24","2012-05-01 14:29:58","0.008"],["2012-05-02 09:21:33","2012-05-02 09:39:27","0.072"]]}
Edited by Ch0cu3r
Link to comment
Share on other sites

if you want the total call duration in each hour for each call number then (similar to Ch0cu3r)

$lines = file('05-2012.txt');
$data = array();
foreach ($lines as $line) {
    list($cno,,$sdate,,$durn) = explode("\t", $line);
    $hr = date('m-d-H', strtotime($sdate));
    if (!isset($data[$cno][$hr])) {
        $data[$cno][$hr] = 0;
    }
    $data[$cno][$hr] += $durn;
}
echo '<pre>',print_r($data, true),'</pre>';

/* results *****************************************
Array
(
    [19738424064] => Array
        (
            [05-01-09] => 120
            [05-01-12] => 1080
            [05-01-14] => 120
            [05-02-09] => 780
            [05-02-14] => 420
        )

    [19738424065] => Array
        (
            [05-01-14] => 420
            [05-02-09] => 1080
        )

)
******************************************************/
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.