pramodh Posted January 24, 2014 Share Posted January 24, 2014 call number date duration 19738424064 2012-05-01 09:04:17 0.00819738424064 2012-05-01 12:01:52 0.07219738424064 2012-05-01 14:27:30 0.00419738424064 2012-05-01 14:28:02 0.00419738424065 2012-05-01 14:14:17 0.0219738424065 2012-05-01 14:28:24 0.00819738424064 2012-05-02 09:20:19 0.00419738424064 2012-05-02 09:08:04 0.04819738424065 2012-05-02 09:21:33 0.07219738424064 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 Quote Link to comment https://forums.phpfreaks.com/topic/285629-find-duplicate-value-in-text-file-first-column-and-merge-line-value-to-it/ Share on other sites More sharing options...
Ch0cu3r Posted January 24, 2014 Share Posted January 24, 2014 (edited) 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 January 24, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285629-find-duplicate-value-in-text-file-first-column-and-merge-line-value-to-it/#findComment-1466384 Share on other sites More sharing options...
Barand Posted January 24, 2014 Share Posted January 24, 2014 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 ) ) ******************************************************/ Quote Link to comment https://forums.phpfreaks.com/topic/285629-find-duplicate-value-in-text-file-first-column-and-merge-line-value-to-it/#findComment-1466390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.