PHP脚本将Json转换为CSV [英] PHP script to convert Json to CSV

查看:141
本文介绍了PHP脚本将Json转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API JSON服务,需要创建一个脚本将数据导出到CSV文件. 是否有人有PHP脚本将JSON迁移为CSV格式?

I've got a API JSON service and need to create a script to export data to CSV files. Does anyone have a php script to migrate JSON to CSV format?

示例Json文件

  {"packWidth":200,
  "itemNo":"SEH404",
  "groupItemNo":"SEH404",
  "status":1,
  "categoryId":24356,
  "packType":"ColorBox",
  "barcode":"1234567890987",
  "modelLabel":"Color",
  "modelList":[{"key":"SEH404","value":"Black"},{"key":"SEH404W","value":"White"}],
  "packQty":20,
  "packInclude":"USB Adapter, USB Charger, Earphone, Leather Case",
  "id":3456}

一个重要的部分是带有标签"modelList"的数组

An important part is the array with the tag "modelList"

数组modelList的结果必须是每个值的一列

The result of the array modelList, must be a column for each value

示例:

packWidth,itemNo,groupItemNo,状态,categoryId,packType,条形码,modelLabel,modelList1,modelList2,packQty,packInclude,id

packWidth, itemNo, groupItemNo, status, categoryId, packType, barcode, modelLabel, modelList1, modelList2, packQty, packInclude, id

200,SEH404,SEH404、1、24356,ColorBox,1234567890987,颜色, SEH404:黑色,SEH404W:白色,20,USB适配器...,3456

200, SEH404, SEH404, 1, 24356, ColorBox, 1234567890987, Color, SEH404:Black, SEH404W:White, 20, USB Adapter... , 3456

某些产品可能还包含5条记录"modelList1-modelList2 modelList3-modelList4-modelList5.没有modelList的产品会将modelList记录为空.

Some products may also contain 5 records "modelList1-modelList2 modelList3-modelList4-modelList5. Products without modelList will record modelList empty.

推荐答案

请尝试此操作. 假设$ json是原始数据,所以我在开始时对其进行了解码.

Please try this. Assuming $json is the raw data, so I decoded it at the beginning.

$json = json_decode($json);

// Will use $csv to build our CSV content
$csv = array();

// Need to define the special column
$modelList = 'modelList';

// Find necessary additional column number for modelList
$maxSubData = 0;
foreach ($json as $id => $data)
{
    $maxSubData = max(array(count($data->modelList), $maxSubData ));
}

// Headers for CSV file
// Headers Start
$headers = array();
foreach ($json[0] as $key => $value)
{
    if ($key == $modelList)
    {
        for ($i = 1; $i <= $maxSubData; $i++)
        {
            $headers[] = $modelList . $i;
        }
    }
    else
    {
        $headers[] = $key;
    }
}
$csv[] = '"' . implode('","', $headers) . '"';
// Headers End

// Now the rows
// Rows Start
foreach ($json as $data)
{
    $fieldValues = array();
    foreach ($data as $key => $value )
    {
        if ($key == $modelList)
        {
            $j = 0;
            foreach ($value as $subValue)
            {
                $subData = array();
                foreach ($subValue as $subCols)
                {
                    $subData[] = $subCols;
                }
                $j++;
                $fieldValues[] = htmlspecialchars(implode(':', $subData ));
            }
            for ($i = $j + 1; $i <= $maxSubData; $i++)
            {
                $fieldValues[] = '';
            }
        }
        else
        {
            $fieldValues[] = htmlspecialchars($value);
        }
    }

    $csv[] = '"' . implode('","', $fieldValues) . '"';
}
// Rows End

$finalCSV = implode("\n", $csv);

print $finalCSV;

这篇关于PHP脚本将Json转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆