普通网友 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程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置