1

I have several arrays stored in a variable. My variable holds these arrays:

Array ( [0] => firstname [1] => lastname [2] => email [3] => phone ) 
Array ( [0] => Benny [1] => Mose [2] => [email protected] [3] => 12345678 ) 
Array ( [0] => Luke [1] => Skywalker [2] => [email protected] [3] => 10000000 ) 
Array ( [0] => Master [1] => Yoda [2] => [email protected] [3] => 13370002 ) 
Array ( [0] => Ben [1] => Kenobi [2] => [email protected] [3] => 13370004 ) 
Array ( [0] => Darth [1] => Bandon [2] => [email protected] [3] => 55554444 ) 
Array ( [0] => C-3PO [1] => Robot [2] => [email protected] [3] => 33339999 ) 
Array ( [0] => R2D2 [1] => Robot [2] => [email protected] [3] => 44447777 ) 
Array ( [0] => Han [1] => Solo [2] => [email protected] [3] => 99998888 ) 
Array ( [0] => Jar jar [1] => Binks [2] => [email protected] [3] => 111112222 ) 

I want to display the first[0] and third[2] value for every array.

I'm able to display the first value for each array by using:

$CSVfp = fopen("csv-list.csv", "r");
if($CSVfp !== FALSE) {
 while(! feof($CSVfp)) {
  $data = fgetcsv($CSVfp);

echo '<hr>';
foreach($data as $arr)
{
    echo $data[0].' '.$data[1].', '.$data[2].'<br>'; // first element

}

 }
}
fclose($CSVfp);

But I can't figure out how to display both first and third value.

0

1 Answer 1

1

A simple foreach loop should do the trick:

foreach($data as $arr)
{
    echo $arr[0]."\r\n"; // first element
    echo $arr[2]."\r\n"; // third element.
}

In a foreach loop, you take each element (in your case which i already an array) and can use it as whatever it is.

So in the first loop, $arr can be seen as:

Array ( [0] => firstname [1] => lastname [2] => email [3] => phone ) 

So you display the elements as normal.

You could do a second foreach loop if you wanted to display every second element in an array with dozens of elements, or search for particular elements or whatever:

foreach($data as $arr)
{
    foreach($arr as $key=>$val)
    {
        echo ($key=='someElement')?$val:"";
    }
}

Edit: Based on your comment, the variable doesn't in fact hold (all the) arrays you showed in your question, but rather only one of them at a time.

Based on that, you can simply access each element of the array:

echo $data[0]."\r\n"; // first element
echo $data[2]."\r\n"; // third element.

Edit 2:

$CSVfp = fopen("csv-list.csv", "r");
if($CSVfp !== FALSE) 
{
    while(! feof($CSVfp)) 
    {
        $data = fgetcsv($CSVfp);
        echo '<hr>';
        echo $data[0].' '.$data[1].', '.$data[2].'<br>'; // first element
    }
}
fclose($CSVfp);

You don't need a foreach at all - you are already accessing each row as a single simple array.

Edit 3:

You can use a simple counter and the modulus function:

$i=0;
$CSVfp = fopen("csv-list.csv", "r");
if($CSVfp !== FALSE) 
{
    while(! feof($CSVfp)) 
    {
        $i++;
        $data = fgetcsv($CSVfp);
        if($i%3==0)
        {
            echo '<hr>';
            echo $data[0].' '.$data[1].', '.$data[2].'<br>'; // first element
        }
    }
}
fclose($CSVfp);
Sign up to request clarification or add additional context in comments.

8 Comments

Following your advice I tried using the simple the foreach loop. But it displays the first and third character of each value. E.g. firstname = fr and so on.
Thanks you were right. But now every entry is displayed 4 times. E.g. Firstname Firstname Firstname Firstname - that doesn't really make sense.
It seems that it is inside another loop that isn't shown in your question. Are you iterating through a loop of database query results?
I'm retrieving the data from a CSV file inside a while loop. See edit.
@KarstenTietje See edit 3. I think that will work, I normally access the filesystem differently, but I would hazzard that will do the trick.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.