php ldap retrieve ou value of dn - php

I'm trying below code to get the value as ou=grp1 from
dn: uid=john,ou=grp1,ou=people,dc=site,dc=com , but not understanding how to retrieve.
here is the 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>';
?>
output:
<pre>Array
(
[uid] => john
[ou] => people //here I want to get output ou=grp1,how?
[dc] => com
)
</pre>
find output here: https://ideone.com/rE6eaH

Because of ou and dc might have multiple values, you should store those values in array. Thanks to that you can have easy access to data. Check out this 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>';
?>
Result is:
Array
(
[uid] => john
[ou] => Array
(
[0] => grp1
[1] => people
)
[dc] => Array
(
[0] => site
[1] => com
)
)

Related

Map array with initial letter in PHP

I wish to group a word list in an array with the initial letter.
function alpha($str) {
$result[substr($str,0,1)] = $str;
return $result;
}
$a = ['abc','cde','frtg','acf'];
$b = array_map('alpha', $a);
print_r($b);
What I need:
Array
(
[a] => abc,acf
[c] => cde
[f] => frtg
)
What I get:
Array
(
[0] => Array
(
[a] => abc
)
[1] => Array
(
[c] => cde
)
[2] => Array
(
[f] => frtg
)
[3] => Array
(
[a] => acf
)
)
How about that :
$answer = [];
$a = ['abc','cde','frtg','acf'];
foreach($a as $word){
$key = substr($word,0,1);
if (isset($answer[$key])){
$answer[$key] .= "," . $word;
} else {
$answer[$key] = $word;
}
}
Just add a variable $c and loop over arrays of array using two foreach and group by alphabet...
function alpha($str) {
$result[substr($str,0,1)] = $str;
return $result;
}
$a = ['abc','cde','frtg','acf'];
$b = array_map('alpha', $a);
#print_r($b);
$c = [];
foreach ($b as $key => $values) {
foreach ($values as $key => $value) {
if(!isset($c[$key])){
$c[$key]=$value;
}else{
$c[$key].= "," . $value;
}
}
}
echo "<PRE>";
print_r($c);
Outupt:
Array
(
[a] => abc,acf
[c] => cde
[f] => frtg
)
The function array_map maps to the original indexes but you want new indexes and an altered array, if there are more values with the same initial character. Therefore array_map don't work for you. You could create your new array this way:
$a = ['abc','cde','frtg','acf'];
$b = Array();
$c = Array();
foreach( $a as $v )
{
// multidimensional array
$b[substr($v,0,1)][] = $v;
// comma separated string
$c[substr($v,0,1)] = (isset($c[substr($v,0,1)])) ?
$c[substr($v,0,1)].",$v" : $v;
}
If the first character can also be multibyte Unicode such as ° or €, mb_substr() must be used! Solution with foreach:
$result = [];
$a = ['abc','€de','frtg','acf'];
foreach($a as $word){
$key = mb_substr($word,0,1);
$result[$key] = array_key_exists($key,$result)
? ($result[$key].",".$word)
: $word
;
}
Solution with array_reduce():
$result = array_reduce($a,function($carry,$item){
$key = mb_substr($item,0,1);
$carry[$key] = array_key_exists($key,$carry) ? ($carry[$key].",".$item) : $item;
return $carry;
},[]);
var_export($result);
Output:
array (
'a' => 'abc,acf',
'€' => '€de',
'f' => 'frtg',
)
I think your intention is to store each word into arrays according to its first letter. In this case, the multidimensional array is the right choice.
$array = ['abc','cde','frtg','acf'];
$new_array = array();
foreach($array as $v){
$letter = substr($v,0,1);
if(!isset($new_array[$letter])) {$new_array[$letter] = array();}
array_push($new_array[$letter], $v);
}
print_r($new_array);

Get array from string in pattern - key1:val1,val2,..;key2:val1,

I would like to get from a string like this
color:blue,red;size:s
to an associative multiarray
[
color => [blue,red],
size => [s]
]
I tried with ([a-z]+):([a-z^,]+) but it's not enough; I don't know how to recursive it or something.
I wouldn't use regular expressions for something like this. Instead use explode() several times.
<?php
$str = 'color:blue,red;size:s';
$values = explode(';', $str);
$arr = [];
foreach($values as $val) {
$parts = explode(':', $val);
$arr[$parts[0]] = explode(',', $parts[1]);
}
Output:
Array
(
[color] => Array
(
[0] => blue
[1] => red
)
[size] => Array
(
[0] => s
)
)
$dataText = 'color:blue,red;size:s';
$data = explode(';', $dataText);
$outputData = [];
foreach ($data as $item){
$itemData = explode(':', $item);
$outputData[$itemData[0]] = explode(',', $itemData[1]);
}
print_r('<pre>');
print_r($outputData);
print_r('</pre>');
With regex is not so simple like explode, but you can try this...
$re = '/(\w+)\:([^;]+)/';
$str = 'color:blue,red;size:s';
preg_match_all($re, $str, $matches);
// Print the entire match result
$result = array();
$keys = array();
for($i = 1; $i < count($matches); $i++) {
foreach($matches[$i] as $k => $val){
if($i == 1) {
$result[$val] = array();
$keys[$k] = $val;
} else {
$result[$keys[$k]] = $val;
}
}
}
echo '<pre>';
print_r($result);
echo '</pre>';
result
Array
(
[color] => blue,red
[size] => s
)

Covert flat array to Nested Array in PHP

Given the following input:
array('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11')
How can I convert it into this:
array(
'one': array(
'two': 3,
'four': array(5,6,7)
)
'eight': array(
'nine': (
'ten':11
)
}
)
$input = array ('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11');
$result = array ();
foreach ($input as $string) {
$data = array_reverse(explode('/', $string));
$tmp_array = array ();
foreach ($data as $val) {
if (empty($tmp_array)) {
$tmp_array = $val;
} else {
$tmp = $tmp_array;
$tmp_array = array ();
$tmp_array[$val] = $tmp;
}
}
$result = array_merge_recursive($result, $tmp_array);
}
echo "<pre>";
print_r($result);
echo "</pre>";
Output:
Array
(
[one] => Array
(
[two] => 3
[four] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
)
[eight] => Array
(
[nine] => Array
(
[ten] => 11
)
)
)
It would be nice if we saw what you have tried.
$my_array = array('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11');
$result= array();
foreach ($my_array as $val) {
$ref = & $result;
foreach (explode("/", $val) as $val) {
if (!isset($ref[$val])) {
$ref[$val] = array();
}
$ref = & $ref[$val];
}
$ref = $val;
}
var_dump($result);

Array Implode in php

I have an array of the following format:
$var = Array
(
[0] => Array
(
[name] => Harry
)
[1] => Array
(
[name] => Wayne
)
)
Array
(
[0] => Array
(
[name] => Wayne
)
I want to implode this array such that i get it in the format:
Harry,Wayne
Wayne
From What I have Tried I am getting it in format:
Harry,Wayne
Harry,Wayne,Wayne
What I have Tried (Not important as its wrong)
foreach($var as $a){
foreach($a as $b){
}$c[] = $b
}
$imp = implode(',',$c);
$var is fetched from database using fetch_array.
$this->db->select('name');
$this->db->where('id', $Id);
$this->db->from('info');
$row = $this->db->get();
$var = $row->result_array();
where $Id is array containing certain user ids.
foreach($var as $a)
{
unset($temp);
foreach($a as $b)
{
$temp[] = $b['name'];
}
$c[] = implode(",", $temp);
}
// output all the names
foreach ($c as $csvNames)
{
echo $csvNames;
}
Try this.
foreach($var as $a){
$m = '';
$delim = '';
foreach($a as $k){
$m .= $delim . $k['name'];
$delim = ',';
}
$c[] = $m;
}
foreach($c as $d){
echo $d;
}
Please ignore those hard-coded loops. There is a recursive function for it.
array_walk_recursive($var, create_function('$val, $key', 'array_push($obj, $val);'), &$output);
echo implode(",",$output);

I'm trying to sort a PHP Array, but can't figure out how

How do you convert an array in PHP that looks like this:
Array (
[2] => B.eot
[3] => B.ttf
[4] => CarnevaleeFreakshow.ttf
[5] => CarnevaleeFreakshow.eot
[6] => TRASHED.ttf
[7] => sub.ttf
)
To look like this:
Array(
[B]=>array(
[0] => B.eot
[1] => B.ttf
)
[CarnevaleeFreakshow]=>array(
[0] => CarnevaleeFreakshow.ttf
[1] => CarnevaleeFreakshow.eot
)
[TRASHED]=>array(
[0] => TRASHED.ttf
)
[sub]=>array(
[0] => sub.ttf
)
)
Is there a name for doing something like this?
the data is being retrieved from a
scandir
array.
<?php
$data = array (
2 => 'B.eot',
3 => 'B.ttf',
4 => 'CarnevaleeFreakshow.ttf',
5 => 'CarnevaleeFreakshow.eot',
6 => 'TRASHED.ttf',
7 => 'sub.ttf'
);
$new_data = array();
foreach ( $data as $value ) {
$tmp = explode( '.', $value );
$ext = '';
if ( $tmp[1] ) $ext = '.' . $tmp[1];
$new_data[ $tmp[0] ][] = $tmp[0] . $ext;
}
print_r( $new_data );
?>
Here is an example.
It can be written shorter, but I think this is the most instructive.
$ARRraw = array (
"B.eot",
"B.ttf",
"CarnevaleeFreakshow.ttf",
"CarnevaleeFreakshow.eot",
"TRASHED.ttf",
"sub.ttf"
) ;
$sorted = array();
foreach($ARRraw as $one){
$firstPoint = strpos($one,".");
// No point? then skip.
if (!($firstPoint === false)){
// Get the part before the point.
$myKey = substr($one,0,$firstPoint);
$sorted[$myKey][] = $one;
}
}
Is there a name for doing something like this?
Nope. Anyway, it should be rather simple using a loop:
<?php
$newArray = array( );
foreach( $originalArray as $fontfile ) {
$newArray[basename( $font )][] = $fontfile;
}
echo '<pre>' . print_r( $newArray, true );
To what i know there is no 'simple' method of doing this.
you could build a function to handle it though.
function convertArray($array) {
$newArray = array();
foreach( $array as $item ) {
$newArray[basename($item)] = $item;
}
return $newArray;
}
That should do what your looking for.
Try it:
function subdiv(array $arr) {
$res = array();
foreach($arr as $val) {
$tmp = explode('.', $val);
if(!isset($res[$tmp[0]]))
$res[$tmp[0]] = array();
$res[$tmp[0]][] = $val;
} return $res;
}
use with:
$res = subdiv($array);
var_dump($res);

Categories