By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

inserting file data into double array

P: 130
hello all,

i have a roblam that need some new view.

i have a file that contain ip | country

what i am tring to do is to know how many ips i have from each country.

the file looks like that : | US | US | UK | FR

my out put should say that i have
3 countrys (US,UK,FR)
and in US 2 ips: ip,ip.
in UK 1 ip: ip.
in FR 1 ip: ip.

i mange to create array with string index for all the countrys
but from here i am stock.

please help

here is a bit of code i done so far
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $countrys = array();
  3. $file  = "ip_list.txt";
  4. $handle = @fopen($file,"r");
  5. if ($handle) {
  6.     while(!feof($handle)){
  7.         $line = explode("|",fgets($handle, 4096));
  8.         $countrys[$line[1]]=array();
  9.     }
  10.     echo count($countrys);
  11.     print_r($countrys);
  12. ?>
what i need to do now the way i see it is to
go over the file agian and
Expand|Select|Wrap|Line Numbers
  1. if $line[1] == string_index          //US==US 
  2. then country["US"][0]=
how can it be done?
Feb 26 '08 #1
Share this Question
Share on Google+
1 Reply

Expert 2.5K+
P: 4,258
No, you only read the file once. Then you build an array with the country as key and the sub-array holding the ip addresses.
When done reading, you just walk the array using 'foreach' and print out the contents of the array. Like this[php]<?php
$countrys = array();
$file = "test.txt";
$handle = fopen($file,"r");
if ($handle) {
$line = explode("|",fgets($handle, 4096));
echo count($countrys);
echo '<pre>';print_r($countrys);
foreach($countrys as $key => $arr) {
echo "<br>$key : ";
foreach ($arr as $ipad)
echo "$ipad ";
?>[/php]The output of your sample file will then be:
Expand|Select|Wrap|Line Numbers
  1. 4
  2. US :  
  3. UK :  
  4. FR : 
When you nhave any questions about this code, holler!
Feb 29 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.