Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP在CSV文件中写入复杂数组_Php_Csv_Magento_Zend Framework - Fatal编程技术网

使用PHP在CSV文件中写入复杂数组

使用PHP在CSV文件中写入复杂数组,php,csv,magento,zend-framework,Php,Csv,Magento,Zend Framework,我有下面给定格式的数组数,每个数组都有子数组数。所有这些数组都存储在 $actionDescription = array( 'products' => $productData ) 我尝试了下面的代码,但不起作用 foreach($actionDescription['products'] as $row){ fputcsv($row); } foreac

我有下面给定格式的数组数,每个数组都有子数组数。所有这些数组都存储在

$actionDescription = array(
                        'products' => $productData
                         ) 
我尝试了下面的代码,但不起作用

  foreach($actionDescription['products'] as $row){
         fputcsv($row);
   } 
 foreach($actionDescription['products'] as $row){
    print_r($row);
} 
行正在打印

 Array
 (
[name] => abc 
[price] => 17
[id] => 12
[sku] => BundleWSP
[currency] => INR
[brandname] => 
[categories] => Array
    (
        [0] => Array
            (
                [cat_id] => 2
                [cat_name] => Category
                [parent_cat_id] => 0
            )

        [1] => Array
            (
                [cat_id] => 2
                [cat_name] => Category
                [parent_cat_id] => 0
            )

        [2] => Array
            (
                [cat_id] => 2
                [cat_name] => Category
                [parent_cat_id] => 0
            )

        [3] => Array
            (
                [cat_id] => 3
                [cat_name] => PRODUCTS
                [parent_cat_id] => 2
            )

        [4] => Array
            (
                [cat_id] => 4
                [cat_name] => CAtegory5
                [parent_cat_id] => 3
            )

    )

 )
 Array
(
[name] => efgh
[price] => 7
[id] => 17
[sku] => jhdksi
[currency] => INR

[brandname] => 
[categories] => Array
    (
        [0] => Array
            (
                [cat_id] => 3
                [cat_name] => PRODUCTS
                [parent_cat_id] => 2
            )

        [1] => Array
            (
                [cat_id] => 1
                [cat_name] => Root Catalog
                [parent_cat_id] => 0
            )

        [2] => Array
            (
                [cat_id] => 1
                [cat_name] => Root Catalog
                [parent_cat_id] => 0
            )

        [3] => Array
            (
                [cat_id] => 2
                [cat_name] => Category
                [parent_cat_id] => 0
            )

        [4] => Array
            (
                [cat_id] => 2
                [cat_name] => Category
                [parent_cat_id] => 0
            )

        [5] => Array
            (
                [cat_id] => 4
                [cat_name] => PROTEIN SHAKES
                [parent_cat_id] => 3
            )

    )

 ) 
我希望此数组为CSV格式,如下所示:

 name,price,id,sku,currency,cat_id,cat_name,parent_cat_id
 abc,   17, 12,Bundlewsp,INR,2,    Category,  0
                             2,    Category,  0
                             2,    Category,  0
                             3,    Products,  2

 efgh,  7,  17,jhdski,INR,   3,Products,2
                             1,root_catalog,0
有许多类别。

请尝试以下方法:

$file = fopen("contacts.csv","w");
foreach($actionDescription['products'] as $row){
    $data = $row['name'].','.$row['price'].','.$row['id'].','.$row['sku'].','.$row['currency'].','.$row['brandname'].','.$row['categories'][0]['cat_id'].','.$row['categories'][0]['cat_name'].','.$row['categories'][0]['parent_cat_id'].'\n';
    fputcsv($file,$data);
    $count = 0;
    foreach($row['categories'] as $categories) {
        if($count > 1){
            $data = ',,,,,,'.$categories['cat_id'].','.$categories['cat_name'].','.$categories['parent_cat_id'];
            fputcsv($file,$data);
        }
        $count++;
    }
}
试试这个:

$file = fopen("contacts.csv","w");
foreach($actionDescription['products'] as $row){
    $data = $row['name'].','.$row['price'].','.$row['id'].','.$row['sku'].','.$row['currency'].','.$row['brandname'].','.$row['categories'][0]['cat_id'].','.$row['categories'][0]['cat_name'].','.$row['categories'][0]['parent_cat_id'].'\n';
    fputcsv($file,$data);
    $count = 0;
    foreach($row['categories'] as $categories) {
        if($count > 1){
            $data = ',,,,,,'.$categories['cat_id'].','.$categories['cat_name'].','.$categories['parent_cat_id'];
            fputcsv($file,$data);
        }
        $count++;
    }
}

你好。你似乎已经得到了下面的答案,你是怎么得到的?你好。你似乎已经得到了下面的答案,你是怎么得到的?