will mannix
will mannix

Reputation: 153

How to use a foreach loop on a multidimensional non associative array

I basically read the rows of a csv file employee_data into an array called $data

$dataSrc = "persistence/employee_data.csv";
    $dataFile = fopen($dataSrc, "r") or die("Unable to open file!");
     $i=0;  //index for the array



   while (($data = fgetcsv($dataFile)) !== FALSE) {
  //$data is an array of the csv elements
    print_r($data);
}

    fclose($dataFile);

The elements within the $data array when I inserted it into the print_r function are as follows.

Array ( [0] => JOHN WILLIAMS [1] => 6/8/1998 [2] => 55456434E [3] => 4321 )
Array ( [0] => SARAH JONES [1] => 15/01/1982 [2] => 56834645Q [3] => 1234 )
Array ( [0] => JAMES Brennan [1] => 09/05/1978 [2] => 25689514W [3] => 8575 ) 

There are 3 arrays in this array but there are no individual keys for each array in the array.

When I try to iterate through the array, it gives me a warning.
"Warning: Invalid argument supplied for foreach()"

I know for an array like this

$food = array('Healthy'=>
                        array('Salad', 'Vegetables', 'Pasta'),
               'Unhealthy'=>
                        array('Pizza','Ice cream'));

You would use something like this to access the elements within the array's of the array.

foreach($food as $element => $inner_array)
{
    echo $element;

    foreach($inner_array as $item)
    {

        echo $item;
    }

}

This approach doesn't work for my $data array. How would you approach gaining access to an array like $data?

Upvotes: 2

Views: 1003

Answers (2)

Shahnawaz Kadari
Shahnawaz Kadari

Reputation: 1571

Since fgetcsv() read row by row, so $data is always contain last record of employee which is single dimension array. here you need to store data to another array(making multidimensional array)

Try this...

$dataSrc = "persistence/employee_data.csv";
$dataFile = fopen($dataSrc, "r") or die("Unable to open file!");
$i=0;  //index for the array
//creating $result array for storing each employee data.
$result = [];

while (($data = fgetcsv($dataFile)) !== FALSE) {
  //assign each employee data to $result array. now result will be multidimensional array.
    $result[] = $data;
}
fclose($dataFile);

You can now use

foreach($result as $resultKey => $employees)
{
    //display key of $result.
    echo $resultKey;

    foreach($employees as $data)
   {     
        echo $data;
    }

}

Upvotes: 2

chinloyal
chinloyal

Reputation: 1141

First you should make your $data variable is an array thenarray_push($data, fgetcsv($dataFile)), so you might want to do something like this:

$dataArray = [];
while (($data = fgetcsv($dataFile)) !== FALSE) {
    array_push($dataArray, $data);
}

Then follow this: Since your array is not asociative instead of this:

foreach($food as $element => $inner_array)
{
    echo $element;

    foreach($inner_array as $item)
    {

        echo $item;
    }

}

In your code you wouldn't have => inner_array, yours would look like this:

foreach($food as $element)
{

    foreach($element as $item)
    {

        echo $item;
    }

}

Upvotes: 0

Related Questions