如何将数据从多维数组导出到CSV

时间:2019-04-05 16:36:55

标签: php multidimensional-array fputcsv

Im托盘将一些数据导出到csv。我可以在网络上打印数据,但是当我将其保存到csv文件时,它会显示以下内容:

Column 1,"Column 2"
Array,"Acme (Sample)",0011U00000AnbfaQAB
Array,"Global (Sample)",0011U00000AnbfbQAB

如何摆脱“阵列”?

我在这里获取数据并打印:

foreach ($records as $record)
{
print 'Name :';
print htmlspecialchars($record['Name']);
print ' - ';
print htmlspecialchars($record['Id']);
print '<br/>';
print "\n";
}

这是将数据保存到csv的代码:

$file = fopen('toNavasoft.csv', 'w');

// save the column headers
fputcsv($file, array('Column 1', 'Column 2'));

// save each row of the data
foreach ($records as $record)
{
fputcsv($file, $record);
}

// Close the file
fclose($file);

我刚刚添加了这个:

var_export ($records);

,输出为:

array ( 0 => array ( 'attributes' => array ( 'type' => 'Account', 'url' => '/services/data/v20.0/sobjects/Account/0011U00000AnbfaQAB', ), 'Name' => 'Acme (Sample)', 'Id' => '0011U00000AnbfaQAB', ), 1 => array ( 'attributes' => array ( 'type' => 'Account', 'url' => '/services/data/v20.0/sobjects/Account/0011U00000AnbfbQAB', ), 'Name' => 'Global (Sample)', 'Id' => '0011U00000AnbfbQAB', ), )

我希望输出如下:

Column 1,Column 2
Acme (Sample),0011U00000AnbfaQAB
Global (Sample),0011U00000AnbfbQAB

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码使用 fputcsv

<?php

$result = array(['column1', 'column2']);
 foreach($records as $record){
  $result[] = [$record['Name'],$record['Id']];
 }


$fp = fopen('toNavasoft.csv', 'w');

foreach ($result as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>
  

这里的重点是选择要保留在简历中的数据   文件存入$ result变量,然后继续执行您已经尝试过   做。

希望这对您有所帮助。

答案 1 :(得分:0)

尝试一下:

<properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.12.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.minidev</groupId>
            <artifactId>json-smart</artifactId>
            <version>2.3</version>
            <scope>compile</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0</version>
            <type>jar</type>
        </dependency>
    </dependencies>

    <build>
        <finalName>api</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Dependencies>jdk.unsupported</Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

答案 2 :(得分:0)

您正在打印$record内的额外数组。您说您的输出是:

array ( 0 => array ( 'attributes' => array ( 'type' => 'Account', 'url' => '/services/data/v20.0/sobjects/Account/0011U00000AnbfaQAB', ), 'Name' => 'Acme (Sample)', 'Id' => '0011U00000AnbfaQAB', ), 1 => array ( 'attributes' => array ( 'type' => 'Account', 'url' => '/services/data/v20.0/sobjects/Account/0011U00000AnbfbQAB', ), 'Name' => 'Global (Sample)', 'Id' => '0011U00000AnbfbQAB', ), )

这意味着您需要在运行$record['attributes']之前取消设置fputcsv()

foreach ($records as $record)
{
  unset($record['attributes']);
  fputcsv($file, $record);
}