可能重复:
Dynamically creating/inserting into an associative array in PHP
我在for循环中生成了以下条目。
CN=Group01,CN=Users,DC=cnn,DC=local - abc@xyz.net
CN=Group04,CN=Users,DC=cnn,DC=local - def@xyz.net
CN=Group02,CN=Users,DC=cnn,DC=local - mlb@xyz.net
CN=Group04,CN=Users,DC=cnn,DC=local - rst@xyz.net
如何在关联数组中排列它们,因此,它看起来像这样:
Array
(
[Group01] => ([0]=>abc@xyz.net),
[Group02] => ([0]=>mlb@xyz.net),
[Group04] => (
[0]=>def@xyz.net,
[1]=>rst.net
)
)
关联数组不需要CN=Users, DC=cnn, DC=local
字符串。
我在for循环中的代码是:
for ($i=0; $i < $entries["count"]; $i++)
{
if (isset($entries[$i]["mail"][0]) && isset($entries[$i]["memberof"][0]))
{
echo $entries[$i]["memberof"][0]." - ".$entries[$i]["mail"][0]."<br />";
}
}
由于
答案 0 :(得分:2)
$info = array();
foreach ($entries as $entry) {
$parts = explode(',', $entry);
$groupName = substr($parts[0], 3);
$emailParts = explode(' - ', $parts[3]);
$email = $emailParts[1];
$info[$groupName][] = $email;
}
答案 1 :(得分:0)
$records = file('yourfile.csv');
$grpUsrArr = array();
foreach( $records as $lineNo => $record );
{
$data = explode( ',', $record );
$grpName = explode( '=', $data[0] );
$usrName = explode( ' - ', $data[3] );
$grpUsr[end( $grpName )][]=end( $usrName );
}