【问题标题】:PHP convert json into csv with sub arrayPHP使用子数组将json转换为csv
【发布时间】:2016-03-18 14:03:33
【问题描述】:

我正在创建 php 代码来将下面的 json 转换为 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
)

我一直在使用下面的代码将其导出为 .csv

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

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

fclose($fp);

我可以很好地转换它,但我面临一个小问题,即 big_image 和 small_image 的子数组没有出现在输出文件 .csv 中(该行为空)

       [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
                        )

顺便说一句,如果我替换:

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

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

我得到链接图片作为输出,所以我需要将它们合并为一个输出

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

编辑:

这里是输出

编辑 2:

我希望输出是这样的

【问题讨论】:

  • 听起来您需要测试是否is_array(),如果是,则循环遍历这些,即。 if(is_array($fields)){ foreach($fields as $fields2) { fputcsv($fp, $fields2); } else {fputcsv($fp, $fields); }
  • 我已经按照链接更改了我的代码,如下$fp = fopen("output.csv","w"); foreach ($json as $fields) { $iresult = []; array_walk_recursive($fields, function($item) use (&$iresult) { $iresult[] = $item; }); fputcsv($fp, $iresult); } 现在正在工作,但它们都在一行中,我想几乎不需要编辑

标签: php csv fputcsv


【解决方案1】:

您可以创建一个函数来确保这些嵌套数组是扁平的。您可以为此创建一个实用函数,如下所示:

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

然后这样称呼它:

$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
    fputcsv($fp, array_flatten($fields));
}
fclose($fp);

【讨论】:

  • 谢谢你的努力,实际上我已经添加了第一个代码,但我收到了警告PHP Warning: fputcsv() expects parameter 2 to be array, string given in /var/www/mycall.php on line 36,但在 output.csv 文件中只写入了大小的图像路径......第二个代码我收到错误PHP Warning: fputcsv() expects parameter 2 to be array, string given in /var/www/mycall.php on line 37,在 output.csv 文件中没有任何内容。我还想知道为什么我需要为数组图像小和大放置测试数组,因为它应该是他们的?
  • 似乎工作正常,但在链接图片的输出中仍然面临小问题......图片的每个链接都有一行,所以看起来很糟糕,需要将每组图片放在一行。
  • 你的意思是列,而不是行,对吧?你能在你的问题中提供预期的输出吗?
  • 你需要所有的图片网址,还是第一个就足够了。因为将它们放在一列中......我不确定你会如何格式化。
  • 不是所有的图片 url,意味着每个数组都有图片 url 的组。我已经更新了我的问题并添加了输出图片
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多