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

时间:2017-04-29 10:12:58

标签: php csv magento zend-framework

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

$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

有很多类别。

1 个答案:

答案 0 :(得分: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++;
    }
}