2016-05-24 10 views
1

ich Code unten bin versucht, den Wert als ou=grp1 vonphp ldap ou Wert von dn abrufen

dn: uid=john,ou=grp1,ou=people,dc=site,dc=com, zu erhalten, aber nicht verstehen, wie abzurufen.

hier ist der Code:

<?php 

function pairstr2Arr ($str, $separator='=', $delim=',') { 
    $elems = explode($delim, $str); 
    foreach($elems as $elem => $val) { 
     $val = trim($val); 
     $nameVal[] = explode($separator, $val); 
     $arr[trim(strtolower($nameVal[$elem][0]))] = trim($nameVal[$elem][1]); 
    } 
     return $arr; 
} 

// Example usage: 
$string = 'uid=john,ou=grp1,ou=people,dc=site,dc=com'; 
$array = pairstr2Arr($string); 

echo '<pre>'; 
print_r($array); 
echo '</pre>'; 

?> 

Ausgang:

<pre>Array 
(
    [uid] => john 
    [ou] => people //here I want to get output ou=grp1,how? 
    [dc] => com 
) 
</pre> 

Fund Ausgabe hier: https://ideone.com/rE6eaH

Antwort

1

Wegen ou und dc könnten mehrere Werte haben, sollten Sie diese Werte speichern in Array. Dank diesem können Sie leicht auf Daten zugreifen. Schauen Sie sich diesen Code:

<?php 

function pairstr2Arr ($str, $separator='=', $delim=',') { 
    $elems = explode($delim, $str); 

    $arr = array(); 
    foreach($elems as $elem => $val) { 
     $val = trim($val); 
     $tempArray = explode($separator, $val); 

     if(!isset($arr[trim($tempArray[0])])) 
      $arr[trim($tempArray[0])] = ''; 

     $arr[trim($tempArray[0])] .= $tempArray[1].';'; 
    } 

    foreach($arr as $key => $value) 
    { 
     $explodedValue = explode(';', $value); 
     if(count($explodedValue) > 2) 
     { 
      $arr[$key] = $explodedValue; 
      unset($arr[$key][count($explodedValue) - 1]); 
     } 
     else 
      $arr[$key] = substr($arr[$key], 0, -1); 
    } 
    return $arr; 
} 

// Example usage: 
$string = 'uid=john,ou=grp1,ou=people,dc=site,dc=com'; 
$array = pairstr2Arr($string); 

echo '<pre>'; 
print_r($array); 
echo '</pre>'; 

?> 

Ergebnis ist:

Array 
(
    [uid] => john 
    [ou] => Array 
     (
      [0] => grp1 
      [1] => people 
     ) 

    [dc] => Array 
     (
      [0] => site 
      [1] => com 
     ) 
) 
Verwandte Themen