亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 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] => [email protected]                    [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,[email protected],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,[email protected],"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 關注
  • 127 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號