普通网友 2015-12-12 19:47
浏览 53
已采纳

PHP将json转换为带子数组的csv

I was creating php code to convert the below json to csv

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [DESC] => bla bal bal
                    [SOLD] => 0
                    [contact_no] => 1234
                    [title] =>  Hiiiii
                    [price] => 10900
                    [big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/14.jpg
                            [1] => http://example.com/images/user_adv/15.jpg
                        )

                    [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/14.jpg
                            [1] => http://example.com/images/user_adv/small/15.jpg
                        )

                    [tpe] => user
                )

            [1] => Array
                (
                    [DESC] => fo fo fof ofof
                    [SOLD] => 0
                    [contact_no] => 234522
                    [title] => Hellooooo sddf
                    [price] => 0
                    [big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/154.jpg
                            [1] => http://example.com/images/user_adv/144.jpg
                            [2] => http://example.com/images/user_adv/147.jpg
                        )

                    [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/154.jpg
                            [1] => http://example.com/images/user_adv/small/144.jpg
                            [2] => http://example.com/images/user_adv/small/147.jpg
                        )

                    [tpe] => user
                )

        )

    [pis] => 3
    [totals] => 23
    [curpage] => 1
    [total_ads] => 71
)

I've been using the below code to export it to .csv

$fp = fopen("output.csv","w");

foreach ($json['data'] as $fields) {
fputcsv($fp, $fields);
}

fclose($fp);

I can convert it fine, but I face a small issue that the sub array which is big_image & small_image is NOT appearing in the output file .csv (the row is empty)

       [big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/154.jpg
                            [1] => http://example.com/images/user_adv/144.jpg
                            [2] => http://example.com/images/user_adv/147.jpg
                        )

                    [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/154.jpg
                            [1] => http://example.com/images/user_adv/small/144.jpg
                            [2] => http://example.com/images/user_adv/small/147.jpg
                        )

By the way, if I replace:

foreach ($json['data'] as $fields) {

with

foreach ($json['data'][0] as $fields) {

I get the link pictures as output, so I need to merge them as one output

  foreach ($json['data'] as $fields) {
  foreach ($json['data'][0] as $fields2) {

edit :

here the output 1

edit 2 :

i expect the output something like that

2

  • 写回答

1条回答 默认 最新

  • doure5236 2015-12-12 20:27
    关注

    You could make a function to make sure those nested arrays are made flat. You can create a utility function for that, like this:

    function array_flatten ($nonFlat) {
        $flat = array();
        foreach (new RecursiveIteratorIterator(
                new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
            $flat[$k] = $v;
        }
        return $flat;
    }
    

    And call it like this:

    $fp = fopen("output.csv","w");
    foreach ($json['data'] as $fields) {
        fputcsv($fp, array_flatten($fields));
    }
    fclose($fp);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?