resago Posted January 20, 2008 Share Posted January 20, 2008 just an example: I had a 6 meg text file with the following data: Proc Code ICD9 Code - each are alpha numeric here's the fun part, there can be multiple proc codes of the same value, but a gamut of icd9 codes. here is an example of using php's flexibility of array indexing. $codes = array("cpt"=>array(),"icd9"=>array()); $handle = @fopen("codes.txt", "r"); if ($handle) { while (!feof($handle)) { $buffer = rtrim(fgets($handle)); $token = preg_split('/ +/', $buffer); $procs[]= $token[0]; $codes["cpt"][$token[0]]["icd9"][$token[1]]++; } fclose($handle); } $procs=array_unique($procs); sort($procs); because php allows you to used undefined array offsets, this works beautifully. then I just run through the array and get all my values and counts. foreach ($procs as $cpt){ $sum = array_sum($codes["cpt"][$cpt]["icd9"]); $icd9s = array_keys($codes["cpt"][$cpt]["icd9"]); $i=0; foreach ($codes["cpt"][$cpt]["icd9"] as $icd9){ $avg = $icd9 / $sum; $table[] = "$cpt,$icd9s[$i],$icd9,$avg\n"; $i++; } } Quote Link to comment https://forums.phpfreaks.com/topic/86852-frequency-chart/ Share on other sites More sharing options...
Fyorl Posted January 20, 2008 Share Posted January 20, 2008 I think using $array[] is just a shortcut for $array[count($array)]. A lot of languages have variable-length array support. Nice example though. Quote Link to comment https://forums.phpfreaks.com/topic/86852-frequency-chart/#findComment-443909 Share on other sites More sharing options...
resago Posted January 20, 2008 Author Share Posted January 20, 2008 this is actually what I was getting at.. $codes["cpt"][$token[0]]["icd9"][$token[1]]++; the indexs being created were the values in the text file. Quote Link to comment https://forums.phpfreaks.com/topic/86852-frequency-chart/#findComment-443912 Share on other sites More sharing options...
Barand Posted January 20, 2008 Share Posted January 20, 2008 I think using $array[] is just a shortcut for $array[count($array)]. A lot of languages have variable-length array support. Nice example though. $array[] = $something; is a shortcut for array_push($array, $something); IE. add $something as the next element in $array EDIT: Yes, I guess that amounts to $array[count($array)] = $something; Quote Link to comment https://forums.phpfreaks.com/topic/86852-frequency-chart/#findComment-443917 Share on other sites More sharing options...
Fyorl Posted January 20, 2008 Share Posted January 20, 2008 Actually I was thinking about it and your way is probably safer. My version would probably only work as expected for enumerated arrays. Even then I'm not sure if unset() resets array keys so you might still end up with gaps. Quote Link to comment https://forums.phpfreaks.com/topic/86852-frequency-chart/#findComment-443925 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.