Need to access 3 values in a php array containing 20, is a 3d array, I need to access the same value 60 times


username
<?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 writing this code. I've generated a text file from Google Reports and managed to break it into an array and then break each element in the array into another array. However, the problem I'm having now is that I only want to retrieve the 3 elements of the second decomposed array, each of which has 20 elements.
Should I use a for or foreach loop what is the best way to do this? I just 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 the same order, since 11 is the last name and 12 is the first name, I want to use the first name first, so they have to be backwards), but when I run a piece of code, it just outputs a newline character and 0. I'm really just wondering what is the most efficient way to iterate through an array, should it be done in an already established loop?
I was thinking that if I wanted to put this into an existing for loop I could use an if/else loop like:

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

But I don't believe this will work. Should I define a variable to store $cell[2],[11] and [12] and create my if loop based on that variable and then just need to echo that variable? Could this possibly work? Any help would be greatly appreciated, I've looked around on forums for posts similar to this but I can't find anything.

20130912,b875c9b154cf7b8d,[email protected],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,[email protected],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,[email protected],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

Although there is more data, this is what the text file itself looks like. All I want to pull is the email address and name.

George Brighton

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";
}

The above code outputs the following using 3 example lines:

[email protected] E L
[email protected] L F
[email protected] A H

Related