473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

inserting file data into double array

130 100+
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 :

34.34.34.34 | US
34.24.24.24 | US
12.12.12.12 | UK
10.10.10.10 | 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]= 34.34.34.34
how can it be done?
Feb 26 '08 #1
1 1341
ronverdonk
4,258 Expert 4TB
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) {
while(!feof($handle)){
$line = explode("|",fgets($handle, 4096));
$countrys[trim($line[1])][]=$line[0];
}
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 : 34.34.34.34  34.24.24.24  
  3. UK : 12.12.12.12  
  4. FR : 10.10.10.10 
When you nhave any questions about this code, holler!
Ronald
Feb 29 '08 #2

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

Similar topics

9
by: steph_de_marseille | last post by:
I would like to write a x array in a file. If the array has a small numbers of columns I know I can use a loop like: int i,j; double array; FILE *file1; file1=fopen("data.dat","w");...
1
by: lactaseman | last post by:
While I know this is not the correct venue... I realize this is of little to no importance to most out there... however, if I had found this in my initial searches, I would have used this. So, as...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
1
by: Ramper | last post by:
Have a .txt document as: String int int int int int int int
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
6
by: trl10 | last post by:
Hi Everyone, I'm still trying to learn C++ and this is my final project. We have to read sales data from a file into an array, process data in a partially filled array via functions, pass an array...
5
emaghero
by: emaghero | last post by:
Greetings and Salutations, I am currently doing some file I/0 on data that I have and I do not wish to use fstream. I have data stored in its untranslated binary format and I want to read it into...
20
by: dav3 | last post by:
Alright folks I am in need of a lil guidance/assistance here. I have a program which reads in a txt file. This txt file contains lines of the form January 3, 2007, 85.8 Now each line of the txt...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.