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

Link to comment
Share on other sites

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>

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.