如何在 PHP 中用未组织的数组元素组织 CSV 数据?

How to organize CSV data with unorganized array elements in PHP?

我有这个数组,我想将其输出为 CSV 文件。

Array
(
    [dc=example,dc=com] => Array
        (
            [o] => example.com
            [objectclass] => Array
                (
                    [0] => simpleSecurityObject
                    [1] => dcObject
                    [2] => organization
                )

            [dc] => example
        )

    [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
        )

)

目前,当我打印所有数据时,这就是我得到的。

dn,o,objectclass,dc,sn, uid, mail, cn
dc=example,dc=com,example.com,simpleSecurityObject:dcObject:organization,example
uid=newton,dc=example,dc=com,Newton,simpleSecurityObject:dcObject:organization,Newton,newton@ldap.forumsys.com,Isaac Newton

这只是以 CSV 格式打印的所有数据,但按照它们最初在数组中排列的顺序排列。

我想确保所有元素实际匹配它们应该匹配的值。这是我想要的输出:

dn,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

请注意,在第一个元素中,我们没有 sn、uid、mail 和 cn 属性,因此将它们留空。第二个元素缺少 o 和 dc,所以它们是空白的。

而且目前数据也乱了。

我怎么

  1. 确保数据位于正确的位置
  2. 确保所有缺失的字段都保留为“”,而不是直接跳到下一个元素?

这是我当前的代码:

<?php
$movies = array(
  
    "dc=example,dc=com" => array(
      "o" => "example.com", 
      "objectclass" => array("simpleSecurityObject", "dcObject", "organization"),
      "dc" => "example"),
    
  
    "uid=newton,dc=example,dc=com" => array(
      "sn" => "Newton", 
      "objectclass" => array("simpleSecurityObject", "dcObject", "organization"),
      "uid" => "Newton",
      "mail" => "newton@ldap.forumsys.com",
      "cn" => "Isaac Newton")
    
);
echo "<pre>";
print_r($movies);
echo "</pre>";
function echo1($n){
  echo "<pre>";
  echo $n;
  echo "</pre>";
}
$i=0;
$single_val = "";

$attributes_list = array("dn", "objectclass", "cn", "userpassword", "description", "sn", "uid", "mail");

echo "<pre></pre>";
$comma_separated = implode(",", $attributes_list);
echo $comma_separated;


foreach ( $movies as $movie => $val ) {

    //echo "<pre></pre> \n Entry #$i";
    //This prints the name of all the arrays.
    //echo1($movie);
    echo "<pre></pre>";
    echo $movie.",";
    $i = $i + 1;

  //Checks if the value of the key value pair is an array.
  if(is_array($val)){
    //Since it's an array, we access its key value pairs again
    foreach ( $val as $key => $value ) {
      //Check to see if value of key value pair is array
      if(is_array($value)){
        //Prints the name of all the arrays
        //echo $key." ";
        //Prints all key value pairs of the array
        $value_array = implode(":", $value);
        $value_array = rtrim($value_array, ":");
        $value_array = $value_array.",";
        print_r($value_array);
        //If $value isn't an array, just prints the key value pairs
      } else{
          /*This is the original code for easier readability
          $hi = "$key : $value";
          echo1($hi);*/
          
          $lastElement = end($val);

          if($value != $lastElement){
            $single_val = $single_val.$value.",";
            echo $single_val;
            $single_val = "";
          } else{
            echo $value;
          }


        }
      }
    //If $val isn't an array, prints the key value pairs
  } else{
  foreach ( $val as $key => $value ) {
    $hi = "The key is: $key The value is:$value";
    echo1($hi);
    }
  }


}
?>

更改此行

foreach ( $val as $key => $value ) {

有了这个

foreach ($attributes_list as $key){
    $value=$val[$key];

所以你总是打印所有的字段,即使它们是空的,并且都保留在原位。 如果您不想收到有关丢失密钥的警告,只需检查是否存在并在不存在时打印“”。

然后

$value_array = implode(":", $value);
$value_array = rtrim($value_array, ":");
$value_array = $value_array.",";

变成

$value_array = implode(",", $value);
$value_array = "{".$value_array."},";

rtrim 应该没有必要。 Implode 将字符串置于值之间。