如何在PHP中使用无组织的数组元素组织CSV数据?
我有这个数组,我想作为CSV文件输出如何在PHP中使用无组织的数组元素组织CSV数据?,php,arrays,csv,ldap,Php,Arrays,Csv,Ldap,我有这个数组,我想作为CSV文件输出 Array ( [dc=example,dc=com] => Array ( [o] => example.com [objectclass] => Array ( [0] => simpleSecurityObject [1] => dcObje
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,因此这些元素为空
此外,数据目前也出现了问题
我该怎么办
<?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);
}
}
}
?>
更改此行
用这个
因此,即使字段为空,也始终打印所有字段,并且所有字段都保持不变。
如果您不想得到关于丢失密钥的警告,只需检查是否存在,并在不存在时打印“”
然后
变成
rtrim
应该不是必需的<代码>内爆
将字符串置于值之间
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."},";