Page 1 of 2
Taking value from one csv file and checking if it exists in
Posted: Wed Apr 13, 2016 6:36 am
by outbeyond
I have one csv file which looks like this
Stanza 07,41912087207
Stanza 08,41912087208
Stanza 09,41912087209
and another one which looks like this
0101*200,0789240959,centralino,101,2016-03-03 14:02:35,281,1.47,,4178,"La Perla" <41917913577>
0101*200,0789240959,centralino,101,2016-03-03 14:02:35,281,500.12,,4178,"La Perla" <41912087207>
0101*200,0917911974,centralino,101,2016-03-03 14:52:16,0,0,,41,"La Perla" <41917913577>
So I want to take ID value from the first file, check if it exists in the second csv file and if it does, print out values after the sixth comma, in lines above so it would be 500.12 since there is one ID that appears on both of CSV files 41912087207.
Here is my code
Code: Select all
<?PHP
$search= "41912087207";
$linescdr = file('cdr.txt');
$linesstanze = file('stanze.txt');
while (list($key, $line1) = each($linesstanze)) {
$arrr = explode(",", $line1);
while (list($key, $line) = each($linescdr)) {
if(strpos($line, $arrr[1])){
$arr = explode(",", $line);
echo $arr[6];
echo "<br>";
}
}
}
?>
In this line of code
if i put $search which is declared in the beginning of the file, it works, but if I put $arrr[1] which would be IDs from the stanze.txt one by one in the while loop, it does not seem to do anything.
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 6:56 am
by Celauran
You're reading in the entire file, line breaks and all. If you trim $arrr[1] before comparing, you will get the expected result.
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 7:04 am
by outbeyond
Thanks a lot, it works now. So what exactly trim did here? When I print arrr[1] and trim(arrr[1]) seems that output is the same.
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 7:07 am
by Celauran
Line break won't display when printing/echoing. var_dump or look at it in the console. $arrr[1] is actually "41912087207\n"
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 7:20 am
by outbeyond
Thanks, i understood. But it apperas now that script is taking just ID from the first line in the stanze.txt and printing out values from the cdr.txt which have same ID. It doesn't get to the second line in stanze.txt. I am attaching files stanze.txt and cdr.txt
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 8:19 am
by Celauran
As you're using
each(), you need to reset the pointer to the beginning after every traversal of the array. Alternately, just use
foreach().
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 4:10 pm
by outbeyond
It worked, one more question related to this. If I have more than one same IDs in cdr.txt how am I suppose to take sum of the their costs instead of just printing it out one by one?
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 4:20 pm
by Celauran
You could create an array as you iterate over the files. Have the ID be the key and the cost be the value, which you can increment on each pass. Depending on what quantities of data we're talking about and how it's being used, it looks like a database might be a better fit.
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 4:38 pm
by Christopher
Note that there are CSV functions in PHP, such as str_getcsv() and fgetcsv(). Using file() and explode() is fine.
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 4:56 pm
by outbeyond
I cannot use array_keys and array_push at the same time, how am I suppose to increment on each pass?
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 6:00 pm
by Celauran
You shouldn't actually need to use either. Foreach loop, check if key exists, initialize to zero if it doesn't, then add value.
Re: Taking value from one csv file and checking if it exists
Posted: Wed Apr 13, 2016 6:06 pm
by Celauran
Something like this should work (untested but you get the idea)
Code: Select all
<?php
$linescdr = file('cdr.txt');
$linesstanze = file('stanze.txt');
$sums = [];
foreach ($linesstanze as $line1) {
$arrr = explode(",", $line1);
$id = trim($arrr[1]);
if (!array_key_exists($id, $sums)) {
$sums[$id] = 0;
}
foreach ($linescdr as $line) {
if (strpos($line, $id)) {
$arr = explode(",", $line);
$sums[$id] += $arr[6];
}
}
}
print_r($sums);
Re: Taking value from one csv file and checking if it exists
Posted: Fri Apr 15, 2016 2:27 am
by outbeyond
Thanks, it works.

Re: Taking value from one csv file and checking if it exists
Posted: Thu Apr 21, 2016 10:00 am
by thinsoldier
Celauran wrote:Line break won't display when printing/echoing. var_dump or look at it in the console. $arrr[1] is actually "41912087207\n"
Unless you are looking at the view-source view of your web page. This is why I made functions preVarDump and prePrintR which wrap var_dump and print_r in <pre> tags.
Re: Taking value from one csv file and checking if it exists
Posted: Thu Apr 21, 2016 10:05 am
by thinsoldier
Celauran wrote:You shouldn't actually need to use either. Foreach loop, check if key exists, initialize to zero if it doesn't, then add value.
In 15 years I don't think I've ever used anything other than foreach($myArray as $key => $value) and while(). Never had a reason to use each or do/while or even the verbose for loop syntax for($i=0; $i<30; $i++)... Did I go wrong somewhere in my php education to have never used these things?