PHP多维阵列至CSV

发布于 2025-01-25 03:48:05 字数 449 浏览 3 评论 0原文

我一直在旋转车轮,试图在此多维数组中浏览值,以创建一个CSV文件。我的API调用返回以下内容(请参阅图像)。

我如何将这些值放在CSV文件中,显然,标头首先出现,然后是记录行?

即使是我正在使用测试的for for for for的帮助也很有价值,因为它没有返回,但没有错误:

$data = json_encode(array("data" => $report_data));     
foreach ($data->header as $n => $header) {
        $hdr = $header->values->data[0]; 
}   
echo $hdr;

谢谢!

“在此处输入图像说明”

I've been spinning my wheels trying to fetch/loop through the values in this multidimensional array in order to create a csv file. My API call returns the following (see image).

How do I place these values in a csv file where, obviously, the header comes first and then the record lines?

Even help with this foreach I'm using for testing would be of value as it returns nothing, but no error:

$data = json_encode(array("data" => $report_data));     
foreach ($data->header as $n => $header) {
        $hdr = $header->values->data[0]; 
}   
echo $hdr;

Thanks in advance!

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-02-01 03:48:05

假设您的PHP阵列看起来像

$data = [
    'header' => [
        'values' => [
            'data' => [
                'AGENT',
                'TALK TIME',
            ],
        ],
    ],
    'records' => [
        [
            'values' => [
                'data' => [
                    'TIM',
                    '194',
                ],
            ],
        ],
        [
            'values' => [
                'data' => [
                    'KIM',
                    '135',
                ],
            ],
        ],
    ]
];

这样

$fp = fopen('file.csv', 'w');
fputcsv($fp, $data['header']['values']['data']);
foreach ($data['records'] as $record) {
    fputcsv($fp, $fields['values']['data']);
}

Assuming your PHP array looks like this

$data = [
    'header' => [
        'values' => [
            'data' => [
                'AGENT',
                'TALK TIME',
            ],
        ],
    ],
    'records' => [
        [
            'values' => [
                'data' => [
                    'TIM',
                    '194',
                ],
            ],
        ],
        [
            'values' => [
                'data' => [
                    'KIM',
                    '135',
                ],
            ],
        ],
    ]
];

Then this should do the trick

$fp = fopen('file.csv', 'w');
fputcsv($fp, $data['header']['values']['data']);
foreach ($data['records'] as $record) {
    fputcsv($fp, $fields['values']['data']);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文