-1

I have to generate the csv from below array

Array
(
    [sku1] => Array
        (
            [bin1] => 10
            [bin2] => 5
            [bin3] => 10
        )

    [sku2] => Array
        (
            [bin2] => 10
            [bin10] => 9
        )

    [sku3] => Array
        (
            [bin3] => 7
        )

)

and csv format should be as below,

sku1,bin1,10

sku1,bin2,10

sku2,bin2,10

sku2,bin10,9

sku3,bin3,7

How to archive this?

5
  • 2
    Have you tried anything? Do you have code? Commented Apr 24, 2018 at 8:04
  • 4
    How to achieve this - with writing code. Commented Apr 24, 2018 at 8:04
  • Use fputcsv() function for getting the result Commented Apr 24, 2018 at 8:13
  • 2
    So, you want us to write your code? Show us what you tried so far? Commented Apr 24, 2018 at 8:16
  • 1
    Have a look at stackoverflow.com/questions/43954815/… Commented Apr 24, 2018 at 8:16

3 Answers 3

1

Try :

                    $array= [
                        'sku1' => [
                            'bin1' => 10,
                            'bin2' => 5,
                            'bin3' => 10
                        ]
                        ,
                        'sku2' => [
                            'bin2' => 10,
                            'bin10' => 9,
                        ],
                        'sku3' => [
                            'bin3' => 7
                        ]
                    ];

                    $fileContent=[];

                    foreach ($array as  $key =>$value1){
                         foreach ($value1 as $key2 => $value2){
                             $fileContent[]=[$key, $key2, $value2];
                         }
                    }


                    $fp = fopen('file.csv', 'w');

                    foreach ($fileContent as $fields) {
                        fputcsv($fp, $fields);
                    }

                    fclose($fp);
Sign up to request clarification or add additional context in comments.

Comments

1

try : // set header to auto download file in csv foramt

header("Content-Disposition: attachment; filename=\"demo.xls\"");
header("Content-Type: application/vnd.ms-excel;");
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen("php://output", 'w');
//this is your data array to be converted in csv 

$result= 
array('sku1'=>array('bin1'=>10,'bin2'=>5,'bin3'=>10),'
sku2'=>array('bin2'=>10,'bin10'=>3),'sku3'=>array('bin3'=>7));

$final=array();
// now se are converting first array key in to node
foreach($result as $key=>$val){
// now se are converting second array key and value in to node
foreach ($val as $k=>$v){
// merging first array key and second array key values in to single array to 
//make a csv
$final[]=array($key,$k,$v);
}

}

foreach ($final as $data)
{
  //Now we are generating the csv from final array 
  fputcsv($out, $data,"\t");
}   
fclose($out);

2 Comments

Can you format the code and add more text on what the code does?
I have edited the code and give explanation what the code does
0

Try this example using fputcsv() function also read this link for getting an idea about fputcsv()

$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);

Comments

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.