为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
MMTTMM 2023-07-01 15:09:21
我在输出空格和确保所有 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,cndc=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我不太确定如何做两件事:确保所有缺失的属性都替换为“”,以便它显示为空确保正确的属性位于正确的位置。例如,“o”可能是一个条目中的第三个属性,但它可能是另一个条目中的第六个属性。如何确保我的 CSV 数据按以下顺序排列:distinguishedname,o,objectclass,dc,sn,uid,mail,cn这里有一些代码以 CSV 格式输出所有数据,但我不确定如何创建空字段并使它们对齐..输出只是所有数据的打印。
查看完整描述

1 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超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;

}


查看完整回答
反对 回复 2023-07-01
  • 1 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信