need to access 3 values in a php array that contains 20, is a 3d array and I need to access the same values 60 times

StackOverflow https://stackoverflow.com/questions/18787531

Question

<?php

// Open the file
$filename = 'pvemail.txt';
$fp = fopen($filename, 'r'); 
// Add each line to an array
if ($fp) {
   $array = explode("\n", fread($fp, filesize($filename)));
}
//print_r ($array);
for ($c = 0; $c < count($array); $c++){
    $cell = explode(",", $array[$c]);
print_r ($cell);
echo '<br/>';
}

?>

I am currently working on this code. I have taken a text file generated from a Google report, and managed to explode it into an array, and then I've taken each element of the array and exploded that into another array. However, the problem I'm now having is I only want to retrieve 3 elements of the second exploded array and there are 20 elements to each array.
What would be the best way to go about this, should I use a for or foreach loop? I only need to print $cell[2], $cell[11] and $cell[12]. I have tried using:

echo ($cell[2] + " " + $cell[12] + " " + $cell[11]

($cell[11] and $cell[12] are in this order because 11 is a last name and 12 is a first name and I want the first name first so I've had to put them backwards) but when I run that piece of code it just outputs line breaks and 0's. I'm really just wondering what would be the most effective method of looping through the arrays, and should I do it within the loop that I have already established?
I was thinking that if I were to put it inside my existing for loop I could use an if/else loop, something like:

if($cell = $cell[2]){
echo ($cell[2])
};

but i'm not convinced this will work. Should I define a variable to store $cell[2], [11] and [12] in, and create my if loop based on that, and then I would only need to echo the variable? Is that likely to be effective? Any help would be appreciated, I've looked around on the forum for posts similar to this but I haven't been able to find anything.

20130912,b875c9b154cf7b8d,el@pv-eu.com,ACTIVE,30720,1054180015,,,20100902,‌​20130910,20130904,L,E,,,,20130911,2010-09-02 09:11:37,2013-09-10 23:51:21,2013-09-04 03:06:09,2013-09-11 00:41:24 
20130912,66c63753b8188f17,lf@pv-eu.com,ACTIVE,30720,3699701524,,,20110315,201309‌​11,20130911,F,L,,,,19691231,2011-03-15 02:00:31,2013-09-11 00:50:17,2013-09-11 00:52:16,1969-12-31 16:00:00
20130912,bd5ef40689adf9ac,ah@pv-eu.com,ACTIVE,30720,3476851137,,,20110426,201309‌​11,20130910,H,A,,,,20110720,2011-04-26 01:47:56,2013-09-11 16:58:48,2013-09-10 06:20:26,2011-07-20

This is how the text file itself looks, although there is a lot more data. All I'm trying to pull is the email address and name.

Was it helpful?

Solution

Assuming pvemail.txt is a CSV file, does this solve your problem?

$content = file_get_contents('pvemail.txt');
$lines = explode("\n", $content);

header('Content-type: text/plain');

foreach($lines as $line) {
    $values = explode(',', $line);
    echo $values[2], ' ', $values[12], ' ', $values[11], "\n";
}

Using the 3 sample lines, the above code outputs this:

el@pv-eu.com E L
lf@pv-eu.com L F
ah@pv-eu.com A H
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top