1

我有一个$events看起来像的数组

Array
(
    [0] => Array
        (
            [4fe8a21c6f05d60023009d96] => any authentication
        )

    [1] => Array
        (
            [4fe8a21c6f05d60023009d96] => any authentication
        )

    [2] => Array
        (
            [4fe8a21c6f05d60023009d96] => any authentication
        )

    [3] => Array
        (
            [4fe8a21c6f05d60023009d96] => any authentication
        )

    [4] => Array
        (
            [4fe8a21c6f05d60023009d96] => any authentication
        )

我正在使用下面的代码尝试在 CSV 的每一行中放置 user_id 和“任何身份验证”字样。但是,使用我的代码,我看到以下内容(而不是"4fe8a21c6f05d60023009d96 " "any authentication"):

"User_ID    "   "   Event"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"
    "Array  "   "   Array"

我的代码是:

$fp = fopen('events.csv', 'w');
$header = array('User_ID',",",'Event');
fputcsv ($fp, $header, "\t");

foreach ($events as $event) {
    $line = array(array_keys($event),',',$event);
    fputcsv($fp, $line,"\t");
}

fclose($fp);
4

1 回答 1

1

对于您的阵列,请尝试以下解决方案:

<?php
$events = array
(
    '0' => array
    (
        '4fe8a21c6f05d60023009d96' => 'any authentication'
    ),

    '1' => array
    (
        '4fe8a21c6f05d60023009d96' => 'any authentication'
    ),

    '2' => array
    (
        '4fe8a21c6f05d60023009d96' => 'any authentication'
    ),

    '3' => array
    (
        '4fe8a21c6f05d60023009d96' => 'any authentication'
    ),

    '4' => array
    (
        '4fe8a21c6f05d60023009d96' => 'any authentication'
    )
);


$fp = fopen('events.csv', 'w+');
$header = array('User_ID',",",'Event');
fputcsv ($fp, $header, "\t");

foreach ($events as $event) {

    $line = array(key($event),',',$event[key($event)]);
    fputcsv($fp, $line,"\t");
}

fclose($fp);

有关fputcsv查看PHP 的更多详细信息:fputcsv - Manual

于 2015-12-17T05:04:09.747 回答