adding a LDAP attribute with PHP - php

can't figure out what is the right syntax to add an additional attributes to a LDAP entry. When authenticating i get this array:
Array
(
[0] => Array
(
[cn] => Array
(
[0] => Vit Kos
)
[shortname] => Array
(
[0] => vit.kos
)
[uid] => Array
(
[0] => vit.kos
)
[mail] => Array
(
[0] => vit.kos#email.com
)
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => inetOrgPerson
[4] => dominoPerson
)
[givenname] => Array
(
[0] => Vit
)
[userpassword] => Array
(
[0] => password here
)
[sn] => Array
(
[0] => Kos
)
[localadmin] => Array
(
[0] => CN=#SysHQAdmin
)
[mailaddress] => Array
(
[0] => Vit.Kos#email.com
)
[maildomain] => Array
(
[0] => EMAIL
)
[dn] => CN=Vit Kos,OU=###,O=EMAIL
)
)
need to get an additional attribute member to be like
Array (
[uid] => Array
(
[0] => vit.kos
)
[mail] => Array
(
[0] => vit.kos#email.com
)
[member] => Array
(
[0] => MEMBER HERE
)
)
Never worked with LDAP before so it's quite confusing for me. Thanks for the answers.

To assign data to the directory item that you retrieved above, you will perform a "modify" operation with ldap_modify(). This is assuming that the schema of your database allows an attribute called member on this object - which it may not, you cannot simply add attributes to any object as and when you feel like it.
Firstly, you will need to create the entry or entries that will belong to the member attribute, and store them in an array:
$member = array (
0 => "This is some data",
1 => "This is some more data"
);
In order to tell the directory which object we want to modify, we will need it's DN. We can get this from the result of your previous search/list/read operation - the array that you show you have retrieved already (I assume this is stored in a variable called $array):
$dn = $array[0]['dn'];
Now we have all the information we need to perform the modify operation (I assume your connected/bound LDAP resource is held in a variable called $ds):
$result = ldap_modify($ds, $dn, array('member'=>$member));
After this, $result will be a boolean indicating whether the operation was successful or not. If it was unsuccessful, you can get an error message explaining why by calling:
$error = ldap_error($ds);

Related

Add a value to existing array

I have an array of data:
Array ( [0] => stdClass Object
(
[name] => admin
[email] => admin#adminmail.com
[password] => cxvxvcxvcxv
)
)
I want to add a new data to this array so the final array looks like:
Array( [0] => stdClass Object
(
[name] => admin
[email] => admin#adminmail.com
[password] => cxvxvcxvcxv
[enabled] => true
)
)
I tried array_merge(), but it gives me result like this:
Array( [0] => stdClass Object
(
[name] => admin
[email] => admin#adminmail.com
[password] => cxvxvcxvcxv
)
[enabled] => true
)
How do I achieve the result I want?
$myArray[0]->enabled = 'true';
Just add the new field straight into your object and assign the value you want to it. This should do the trick for you.
If you array has multiple nested arrays you will need a loop though. My code will only work for position 0 like you asked.

PHP - Check if Array exists in Multidimensional Array with an Exact Match of Pair

I need to check if an exact pair of two values exists in a multidimensional array together.
I have an array like this:
Array
(
[code] => 200
[response] => Success
[0] => Array
(
[email] => example123#sample.com
[status] => Approved: Printed & Cleared
)
[1] => Array
(
[email] => xxexample123#sample.com
[status] => Pending
)
[2] => Array
(
[email] => example1345#sample.com
[status] => Approved
)
[3] => Array
(
[email] => example1235#sample.com
[status] => Approved: Printed & Cleared
)
)
Then I have an array that looks like this:
Array
(
[email] => xxexample123#sample.com
[status] => Pending
)
I need to check if that exact pair exists in the multidimensional array. Not just that the status and email appear seperate from each other.
You can use array_search() in the same manner if you need to get the key, but at its simplest (assuming $array1 only has the keys and values that are being search for and in the same order):
$array2 = array('email' => 'example123#sample.com',
'status' => 'Pending');
if(in_array($array2, $array1)) {
//yes
} else {
//no
}
See the Demo showing found and not found.

PHP - Re-arrange multi-array so that certain combinations have the same key(index)

I have an array, which looks like this:
Array
(
[sku_0017768] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017766] => Array
(
[0] => ff5a47bd699bcb52944d0727a5b443b6
[1] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017723] => Array
(
[3] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_0017767] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
)
[sku_8402350] => Array
(
[0] => a3c83832b4089d83d164b3cac596fc7e
[1] => fb91d14d405be52ce989acd2918ea1a0
[2] => ff5a47bd699bcb52944d0727a5b443b6
[3] => d14afce16e477663ebe51fdd65cd5010
)
[sku_8402200] => Array
(
[0] => d14afce16e477663ebe51fdd65cd5010
)
)
Now, this array represents a list of SKU's with one or more 'combination codes'. So, in this example 'sku_0017768' matches product sku_0017766 because they share the same combination code.
sku_8402350 has multiple combination as you can see.
What I need to have now is a new array, where the index(key) of the combination code needs to be same among the other products within the combination.
Output should look like this:
Array
(
[sku_0017768] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017766] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
[1] => ff5a47bd699bcb52944d0727a5b443b6
)
[sku_0017723] => Array
(
[0] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_0017767] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
)
[sku_8402350] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
[1] => ff5a47bd699bcb52944d0727a5b443b6
[2] => d14afce16e477663ebe51fdd65cd5010
[3] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_8402200] => Array
(
[2] => d14afce16e477663ebe51fdd65cd5010
)
)
The difficulty I have is that when a key is already in use, the key of the other products within the combination should increase (with 1) as well, until there is an empty key which the combination can use.
Can anybody help me pointing me in the right direction? Feel free to ask if anything is not clear.

How can I extract a PHP string from this sub-array?

The array I am looking at is this:
Array
(
[0] => Array
(
[0] => Bigcommerce\Api\Resources\ProductCustomField Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => product_id
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => product_id
)
[fields:protected] => stdClass Object
(
[id] => 17
[product_id] => 3232
[name] => Artist
[text] => Test
)
[id:protected] => 17
[ignoreIfZero:protected] => Array
(
)
)
)
)
I want to check to see if 'Artist' exists in a php conditional statement. But I don't know how to turn 'Artist' into a string.
UPDATED:
I did not find understand how to extract that value into a string, but I got what I was looking for using the method related to the bigcommerce api:
$customs = Bigcommerce::getProductCustomFields($product->id);
foreach($customs as $custom) {
if($custom->name == 'Artist'): // do something
endif;
}
Ok, looking at the source, it seems you should be able to use the magic __get method. Try
$array[0][0]->name == 'Artist'
The value of the custom field would be stored in the "text" resource for that custom field.
See the following link where you can see the 4 properties of a custom field.
https://developer.bigcommerce.com/api/stores/v2/products/custom_fields

Return value when searching for key in array PHP

I tried to get an answer for this in other posts with no luck, hope someone can help me here, i have a multidimensional array:
Array (
[0] => stdClass Object (
[affectsVersions] => Array ( )
[assignee] => hmontes
[attachmentNames] => Array ( )
[components] => Array ( )
[created] => 2012-08-15T05:31:26.000Z
[customFieldValues] => Array (
[0] => stdClass Object (
[customfieldId] => customfield_10201
[key] => [values] => Array (
[0] => 123456
)
)
[1] => stdClass Object (
[customfieldId] => customfield_10004
[key] => [values] => Array (
[0] => 30
)
)
)
[description] => [duedate] => [environment] => [fixVersions] => Array ( )
[id] => 10228
[key] => NTP-29
[priority] => 3
[project] => NTP
[reporter] => hmontes
[resolution] => [status] => 1
[summary] => case 123456
[type] => 3
[updated] => 2012-08-15T05:31:26.000Z
[votes] => 0
)
)
this is what i get when i do a print_r with the array variable, i need to search and get the value from [key] that would be in this case NTP-29 and keep it in a variable as string.
You can get the value of an array by the key using $array['keyName'];
But, for you it looks like you just need to go deeper $array[0]['key'];
Both arrays values and properties of objects can be accessed using associative array syntax. To get the value of the key property in your object within the array you'd do the following, assuming $array is a variable containing a reference to your array:
$key = $array[0]['key']; // accesses NTP-29 in this case.
Here's another way to access the same property, using object property-access syntax:
$key = $array[0]->key; // also accesses NTP-29.

Categories