我需要避免CN = Users,DC = aab,DC =从数组返回的本地值。然后将剩余名称的INITIALS保存到新数组中。任何帮助,将不胜感激。我真的不确定从哪里开始。 当我执行以下操作时,这就是它现在返回的方式。
$reportees = $_SESSION["user"]->directreports;
$reportees = implode(",", $reportees);
CN = John Doe,CN = Users,DC = aab,DC = local,CN = Jane Ann Doe,CN = Users,DC = aab,DC = local
答案 0 :(得分:1)
$reportees = $_SESSION["user"]->directreports;
$blacklist = array('CN=Users', 'DC=aab', 'DC=local');
$arrayOfInitials = array();
foreach($reportees as $key=>$reportee){
// ignore the unwanted values :
if(!in_array($reportee, $blacklist)){
// take only the value after the "=":
$reportee = explode("=", $reportee);
if(isset($reportee[1])){
$reportee = $reportee[1];
$reporteeNames = explode(" ", $reportee);
$initials = "";
// get the initials :
foreach($reporteeNames as $name){
$initials .= strtoupper($name[0]);
}
$arrayOfInitials[] = $initials;
}
}
}
$initialsAsStr = implode(',', $arrayOfInitials);
var_dump($initialsAsStr);
输出将是:
string(10) "JD,B,JAD,B"
答案 1 :(得分:1)
<?php
$in = ['CN=John Doe','CN=Users','DC=aab','DC=local','CN=Jane Ann Doe','CN=Users','DC=aab','DC=local'];
function initials_from_value($i) {
strtok($i, '=');
$i = strtok('=');
$names = explode(' ', $i);
$initials = array_map(function ($i) { return substr($i, 0, 1); }, $names);
return $initials;
}
$out = array();
foreach($in as $item) {
if(strpos($item, 'CN=') === 0 && $item !== 'CN=Users') {
$out[] = implode(' ', initials_from_value($item));
}
}
var_export($out);
输出:
array (
0 => 'J D',
1 => 'J A D',
)
附录(仅整理首字母和最后字母):
$out = array();
foreach($in as $item) {
if(strpos($item, 'CN=') === 0 && $item !== 'CN=Users') {
if( ($initials = initials_from_value($item)) && count($initials) >= 2) {
$first = reset($initials);
$last = end($initials);
$out[] = $first . $last;
}
}
}
var_export($out);
输出:
array (
0 => 'JD',
1 => 'JD',
)
答案 2 :(得分:0)
您可以使用闭包过滤数组,并为不需要的项返回FALSE,以便可以使用array_filter进行过滤。
通过这种方式,您可以逐步检查结果,我发现这更容易调试。
$values = array_map(
/* Get a reportee, return the name or FALSE */
function($reportee) {
list($key, $value) = explode('=', $reportee);
switch ($key) {
case 'CN': // CN=Users or CN=Jane Ann Doe
if ('Users' === $value) {
return false;
}
// $value is now hopefully Jane Ann Doe.
return $value;
default: // Anything else gets FALSEd.
return false;
}
},
$reportees
);
// Eliminate unwanted results:
// [ 0 => false, 1 => false, 2 => false, 3 => 'Jane Ann Doe', 4 => false ]
$values = array_filter($values);
// $values should now be something like [ 3 => 'Jane Ann Doe' ]
// Get initials.
$values = array_map(
/* input: "Jane Ann Doe"; output: "JAD" */
function ($value) {
$words = preg_split('#\\s+#', $value);
$initials = array_map(
function ($word) {
return substr($word, 0, 1);
},
$words
);
return implode('', $initials);
},
$values
);
// $values is now [ '3' => 'JAD' ].
// You can use array_values($values) to renumber the array.
答案 3 :(得分:0)
以下内容应该有效(PHP&gt; = 5.4):
$reportees = array('CN=John Doe',
'CN=Users',
'DC=aab',
'DC=local',
'CN=Jane Ann Doe',
'CN=Users',
'DC=aab',
'DC=local');
$result = [];
foreach ($reportees as $value) {
switch ($value) {
// ignore these values
case 'CN=Users':
case 'DC=aab':
case 'DC=local':
continue 2;
// get initials of these values
default:
$expr = '/(?<=\s|^)[a-z]/i';
preg_match_all($expr, explode('=', $value)[1], $matches);
$result[] = strtoupper(implode('', $matches[0]));
}
}
$reportees = implode(',', $result);
结果:
JD,JAD