1

I have following array:

categories = [
    [0]=> array(2) { 
        ["categoryTitle"]=> string(int)
        ["subCategories"]=> array(n) { 
            [0]=> string(int)
            ...
            [n]=> string(int)
        }
    }
    [1]=> array(2) { 
        ["categoryTitle"]=> string(int)
        ["subCategories"]=> array(n) { 
            [0]=> string(int)
            ...
            [n]=> string(int)
        }
    }
    [n]=> array(2) { 
        ["categoryTitle"]=> string(int)
        ["subCategories"]=> array(n) { 
            [0]=> string(int)
            ...
            [n]=> string(int)
        }
    }   
]

How do I correctly store this data into CSV file, so I could have following structure:

Category_table                 | SubCategories_table

categories[n]['categoryTitle'] | categories[n]['subCategories']

If I just run this:

$fp = fopen('categories.csv', 'w');
foreach ($categories as $category) {
    fputcsv($fp, $category);
}
fclose($fp);

I end up with structure like this: first table has correct string(title of the category), but next table just has literally word array.

So how can I correctly store the subCategories array in csv, in separate table column?

Martin Heralecký
  • 5,649
  • 3
  • 27
  • 65
Tachi
  • 2,042
  • 3
  • 27
  • 44
  • 1
    [this](http://stackoverflow.com/questions/14125922/php-multidimensional-array-to-csv) could be useful to you – DevDonkey Oct 16 '15 at 08:42
  • Direct array to csv requires a 2-dimensional array (for a loop). The indexes don't matter, just so long as the elements are in the correct order. The first array (second dimension), will be used as the headers. – Flosculus Oct 16 '15 at 08:49

1 Answers1

0

Well I've just used http://php.net/manual/en/function.implode.php, transformed array into string and now store it into csv.

Tachi
  • 2,042
  • 3
  • 27
  • 44