如何从 PHP 中的多维数组输出 CSV 文件中缺失属性的空格

我在输出空格和确保所有 CSV 数据输出到正确的位置时遇到了一些问题,并且想知道如何做到这一点。

我正在尝试将 LDAP 服务器搜索的输出格式化为 CSV 格式,但问题是某些条目的属性乱序,或者某些条目完全缺少属性。

这是一些数据的示例

Array

(

    [0] => Array

        (

            [dc=example,dc=com] => Array

                (

                    [o] => example.com

                    [objectclass] => Array

                        (

                            [0] => simpleSecurityObject

                            [1] => dcObject

                            [2] => organization

                        )


                    [dc] => example

                )


        )


    [1] => Array

        (

            [uid=newton,dc=example,dc=com] => Array

                (

                    [sn] => Newton

                    [objectclass] => Array

                        (

                            [0] => simpleSecurityObject

                            [1] => dcObject

                            [2] => organization

                        )


                    [uid] => Newton

                    [mail] => newton@ldap.forumsys.com

                    [cn] => Isaac Newton

                )


        )


)

请注意,在第一个 ([0]) 数组中,我有一个名为“o”和“dc”的条目,而在第二个数组中,我没有它们。


所以本质上,我想输出这个:

distinguishedname,o,objectclass,dc,sn,uid,mail,cn

dc=example,dc=com,example.com,{simpleSecurityObject,dcObject,organization},example, , , , 

uid=newton,dc=example,dc=com, ,{simpleSecurityObject,dcObject,organization}, , ,Newton,newton@ldap.forumsys.com,Isaac Newton

我不太确定如何做两件事:

  1. 确保所有缺失的属性都替换为“”,以便它显示为空

  2. 确保正确的属性位于正确的位置。例如,“o”可能是一个条目中的第三个属性,但它可能是另一个条目中的第六个属性。如何确保我的 CSV 数据按以下顺序排列:

distinguishedname,o,objectclass,dc,sn,uid,mail,cn

这里有一些代码以 CSV 格式输出所有数据,但我不确定如何创建空字段并使它们对齐..输出只是所有数据的打印。


MMTTMM
浏览 112回答 1
1回答

小怪兽爱吃肉

以下代码创建一个模板数组,以便每个记录包含所有值,然后用于array_merge()将每个记录中的值复制到模板中。任何不在记录中的值都将保留为空,以确保保持对齐(代码注释中有更多描述)...// Start with the column list and split into an array$headers = explode(',', 'distinguishedname,o,objectclass,dc,sn,uid,mail,cn');// Create template record with the fields you want to end up with$header = array_fill_keys($headers, null);$output = [];foreach ( $details as $detail ) {    foreach ( $detail as $name => $entry )  {        // Set the values in the template header from the current entry        $newEntry = array_merge( $header, $entry );        // Add the name in        $newEntry ['distinguishedname'] = $name;        // Process the object class from an array to a string        $newEntry ['objectclass'] = "{".implode(",", $newEntry ['objectclass'])."}";        $output [] = $newEntry;    }}// Write file out$fh = fopen("ad.csv", "w");fputcsv($fh, $headers);foreach ( $output as $row ) {    fputcsv($fh, $row);}fclose($fh);样本数据给出......distinguishedname,o,objectclass,dc,sn,uid,mail,cn"dc=example,dc=com",example.com,"{simpleSecurityObject,dcObject,organization}",example,,,,"uid=newton,dc=example,dc=com",,"{simpleSecurityObject,dcObject,organization}",,Newton,Newton,newton@ldap.forumsys.com,"Isaac Newton"请注意,使用的fpustcsv()优点是,如果字段包含逗号(分隔符),那么它会将它们放在引号中以确保它们只是一个字段。编辑:$headers = explode(',', 'distinguishedname,o,objectclass,dc,sn,uid,mail,cn');$output = [];foreach ( $details as $name => $entry )  {    echo $name.PHP_EOL;    $newEntry = [];    foreach ( $headers as $header ) {        $value = $entry[$header] ?? null;        if ( is_array($value) ) {            $value = "{".implode(",", $value)."}";        }        $newEntry [$header] = $value;    }    $newEntry ['distinguishedname'] = $name;    $output [] = $newEntry;}
打开App,查看更多内容
随时随地看视频慕课网APP