Taking value from one csv file and checking if it exists in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

outbeyond
Forum Newbie
Posts: 7
Joined: Wed Apr 13, 2016 6:32 am

Taking value from one csv file and checking if it exists in

Post 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

Code: Select all

if(strpos($line, $arrr[1])){
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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Taking value from one csv file and checking if it exists

Post 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.
outbeyond
Forum Newbie
Posts: 7
Joined: Wed Apr 13, 2016 6:32 am

Re: Taking value from one csv file and checking if it exists

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Taking value from one csv file and checking if it exists

Post 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"
outbeyond
Forum Newbie
Posts: 7
Joined: Wed Apr 13, 2016 6:32 am

Re: Taking value from one csv file and checking if it exists

Post 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
Attachments
new.rar
(920 Bytes) Downloaded 551 times
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Taking value from one csv file and checking if it exists

Post 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().
outbeyond
Forum Newbie
Posts: 7
Joined: Wed Apr 13, 2016 6:32 am

Re: Taking value from one csv file and checking if it exists

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Taking value from one csv file and checking if it exists

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Taking value from one csv file and checking if it exists

Post by Christopher »

Note that there are CSV functions in PHP, such as str_getcsv() and fgetcsv(). Using file() and explode() is fine.
(#10850)
outbeyond
Forum Newbie
Posts: 7
Joined: Wed Apr 13, 2016 6:32 am

Re: Taking value from one csv file and checking if it exists

Post by outbeyond »

I cannot use array_keys and array_push at the same time, how am I suppose to increment on each pass?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Taking value from one csv file and checking if it exists

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Taking value from one csv file and checking if it exists

Post 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);
outbeyond
Forum Newbie
Posts: 7
Joined: Wed Apr 13, 2016 6:32 am

Re: Taking value from one csv file and checking if it exists

Post by outbeyond »

Thanks, it works. :aye:
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Taking value from one csv file and checking if it exists

Post 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.
Warning: I have no idea what I'm talking about.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Taking value from one csv file and checking if it exists

Post 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?
Warning: I have no idea what I'm talking about.
Post Reply