Jump to content

sorting and counting numbers from a text document.


Jragon

Recommended Posts

Hello,

I was wondering how i could count how many times the number 1  appers in the document and 2, 3, 4, 5, 6, 7, 8, 9, 10.

 

The text will have over 1000000 results set out like this:

2
10
1
4
2
7
5
3
8
10
1
7
7
2
1
9
3
3
7
3
1
5
2
1
4
7
7
9
10
2
5
8
10
9
8
3
2
2
9
6
8
8
8
2
5
1
9
3
5
9
3
4
6
7
5
6
9
2
3
6
2

 

once it has counted the charicters i want it to put it in to a table

 

I have all redey made the table:

<table border="1">
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
</tr>
<tr>
<td>number of numbers</td>
<td>number of numbers</td>
<td>number of numbers</td>
<td>number of numbers</td>
<td>number of numbers</td>
<td>number of numbers</td>
<td>number of numbers</td>
<td>number of numbers</td>
<td>number of numbers</td>
<td>number of numbers</td>
</tr>
</table> 

 

I have looked at the count_chars function but the manule is ver confuzerling

 

if anyone could explane how i could do this it would be great

 

Thanks

 

Jragon

Read your text file with file. Now use array_count_values to count how many times each number is stated

<?php

$numbers = file('numbers.txt', FILE_IGNORE_NEW_LINES);

$results = array_count_values($numbers);
ksort($results);

$fields = array_keys($results);

?>
<style type="text/css">
th, td { width: 50px; }
</style>
<table border="1">
   <tr>
        <th><?php echo implode('</th><th>', $fields) ?></th>
    </tr>
    <tr>
        <td><?php echo implode('</td><td>', $results) ?></td>
    </tr>
</table>

Is it possable to make a graph with php so that its sort of a line graph. saying how many times it has occerd like this graph:

http://img840.imageshack.us/i/rest.png/

 

done the bottom there is the numbers and along the side is the ammount of times the number has occerd

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.