I am trying to create multiple csv from a given source (i.e array).
It works with sample data but it does not work with the actual data.
here is the code :-
<?php
$keycompare = array('98','83');
$arraysource =
array (
0 =>
array (
0 =>
array (
1 => 'Coogee Tri Club Performance Cycle Jersey Women',
2 => 'WCJ011DC-15924v',
3 => '2',
4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
5 => '98',
),
1 =>
array (
1 => 'Coogee Tri Club Cycle Bib and Brace Men',
2 => 'MCB009DC-15925v',
3 => '6',
4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
5 => '98',
),
2 =>
array (
1 => 'Coogee Tri Club Performance Cycle Jersey Men',
2 => 'MCJ007DC-15924v',
3 => '12',
4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
5 => '98',
),
3 =>
array (
1 => 'Coogee Tri Club Performance Triathlon Suit Men/Unisex',
2 => 'MTU018DC-14792v',
3 => '5',
4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
5 => '98',
),
4 =>
array (
1 => 'Coogee Tri Club Triathlon Shorts Men',
2 => 'UTK006DC-14790v',
3 => '1',
4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
5 => '98',
),
5 =>
array (
1 => 'Coogee Tri Club Run Singlet Men',
2 => 'MRS004DC-17445v',
3 => '12',
4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
5 => '98',
),
6 =>
array (
1 => 'Coogee Tri Club Run Singlet Women',
2 => 'WRT004DC-17445v',
3 => '5',
4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
5 => '98',
),
7 =>
array (
1 => 'Cogee Tri Club Visor',
2 => 'BHATDC-17444v',
3 => '12',
4 => 'Coogee Triathlon Club - Rd 2',
5 => '98',
),
8 =>
array (
1 => 'Coogee Tri Club Chrlorine Resistant Two Piece Swim Suit Women',
2 => 'WSU007DC-13856v',
3 => '3',
4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
5 => '98',
),
9 =>
array (
1 => 'Coogee Tri Club Swim Suit Women',
2 => 'WSU009DC-13851v',
3 => '1',
4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
5 => '98',
),
10 =>
array (
1 => 'Coogee Tri Club Elite Triathlon Suit Hydrophobic Women',
2 => 'WTU022DC-14029v',
3 => '2',
4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
5 => '98',
),
11 =>
array (
1 => 'Coogee Tri Club Performance Sleeved Triathlon Suit Women',
2 => 'WTU032DC-14789v',
3 => '3',
4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
5 => '98',
),
12 =>
array (
1 => 'Coogee Tri Club Performance Sleeved Triathlon Suit Men',
2 => 'MTU021DC-14789v',
3 => '3',
4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
5 => '98',
),
13 =>
array (
1 => 'Coogee Tri Club Cycle Bib and Brace Women',
2 => 'WCB010DC-15925v',
3 => '3',
4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
5 => '98',
),
14 =>
array (
1 => 'Coogee Tri Club Swim Briefs Men',
2 => 'MSU001DC-14557v',
3 => '4',
4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
5 => '98',
),
),
1 =>
array (
15 =>
array (
1 => 'Redcliffe Triathlon Club Triathlon Jersey Womens',
2 => 'WTJ002DC-16580v',
3 => '4',
4 => 'Private Shops,Redcliffe Triathlon Club,Womens size chart',
5 => '83',
),
16 =>
array (
1 => 'Redcliffe Triathlon Club Triathlon Knicks Mens',
2 => 'UTK006DC-14316v',
3 => '3',
4 => 'Private Shops,Redcliffe Triathlon Club,Mens size chart',
5 => '83',
),
17 =>
array (
1 => 'Redcliffe Triathlon Club Triathlon Shorts Women',
2 => 'WTK005DC-14316v',
3 => '2',
4 => 'Private Shops,Redcliffe Triathlon Club,Womens size chart',
5 => '83',
),
),
);
// some data to be used in the csv files
$headers = array('id', 'name', 'age', 'species');
$records = array(
array('1', 'gise', '4', 'cat'),
array('2', 'hek2mgl', '36', 'human')
);
// create your zip file
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
// loop to create 3 csv files
for ($i = 0; $i < 3; $i++) {
// create a temporary file
$fd = fopen('php://temp/maxmemory:1048576', 'w');
if (false === $fd) {
die('Failed to create temporary file');
}
// write the data to csv
fputcsv($fd, $headers);
foreach($arraysource as $record) {
fputcsv($fd, $record);
}
// return to the start of the stream
rewind($fd);
// add the in-memory file to the archive, giving a name
$zip->addFromString('file-'.$i.'.csv', stream_get_contents($fd) );
//close the file
fclose($fd);
}
// close the archive
$zip->close();
i am not sure where is the issue if its between array format or something.
Basically the expected output is 2 csv
csv one will have data from array having key value as 5 => '98'
csv two will have data from array having key value as 5 => '83'
Can you please give me some hint on this ?
Thankyou