2016-07-06 3 views
1

CN = Users、DC = aab、DC =配列から返されるローカル値を避ける必要があります。その後、残りの名前のINITIALSを新しい配列に保存します。どんな助けもありがとう。私はどこから始めるべきか本当に分かりません。 これは、私が以下のことをしたときに今返す方法です。配列の特定の項目を無視して残りの部分からイニシャルを作成する必要があります

$reportees = $_SESSION["user"]->directreports; 
$reportees = implode(",", $reportees); 

CN =ジョン・ドウ、CN =ユーザー、DCは、AAB、DC =ローカル、CN =ジェーン・アン・ドウを=、CNは=ユーザー、DCは、AABを=、DC =ローカル

答えて

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" 
+0

、ありがとうございました。 – Ryan

+0

イニシャル「B」はどこから来ますか? –

+0

投稿が編集されたため、追加のエントリ "DC = byl"がありました。 –

0

あなたは、配列をフィルタリングすることができますそれはそれがarray_filterでフィルタリングできるように、不要な項目に対してFALSEを返します。

この方法で、結果を段階的に確認することができます。これは簡単にデバッグすることができます。

$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. 
0

以下は(PHP> = 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 
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); 

出力:この1つは完全に働いた

array (
    0 => 'JD', 
    1 => 'JD', 
) 
+0

これもうまくいった、私は文字の間からスペースを取らなければならなかった。ありがとうございました。 – Ryan

+0

ミドルネームの頭文字はどうしたらいいですか?だからそれは姓と名を出力するだけです – Ryan

+1

@ライアン、あなたは簡潔に変更されています!追加された例。 – Progrock

関連する問題