Flatten PHP stdClass object to array maintaining array dimensions - php

I am working with soap responses that contain nested wrappers and whose child(ren) have nested properties.
I am trying to flatten these response to:
remove the wrappers
flatten the children
maintain the individual children (dimensions)
I am currently working with the following that achieves #1 and #3 however it does not flatten the inner children. Note that $this->response is converted from a stdClass to Array before being flattened.
How can I also flatten down the inner nested child elements?
private function toArray()
{
$this->response = json_decode(json_encode($this->response), true);
return $this;
}
private function flatten($array = null)
{
if (is_null($array)) {
$array = $this->response;
}
if (is_array($array)) {
foreach ($array as $k => $v) {
if (count($v) == 1 && is_array($v)) {
return $this->flatten($v);
}
if (isset($v[0])) {
$this->response = $v;
return $this;
}
unset($this->response);
$this->response[] = $v;
return $this;
}
}
}
...which will transform this:
stdClass Object
(
[ArrayOfDevice] => stdClass Object
(
[Device] => Array
(
[0] => stdClass Object
(
[NamedElement] => stdClass Object
(
[Element] => stdClass Object
(
[ElementType] => DEVICE
[id] => Device1ID
)
[name] => Device1
)
[hostName] => Device1.hostname
[ipAddress] => Device1.ip
[location] => location1
[modelName] =>
[modelNumber] =>
[parentID] => xxxYYY
[serialNumber] => 123456789
)
[1] => stdClass Object
(
[NamedElement] => stdClass Object
(
[Element] => stdClass Object
(
[ElementType] => DEVICE
[id] => Device2ID
)
[name] => Device2
)
[hostName] => Device2.hostname
[ipAddress] => Device2.ip
[location] => location1
[modelName] =>
[modelNumber] =>
[parentID] => xxxYYY
[serialNumber] => 987654321
)
)
)
)
...to this:
Array
(
[0] => Array
(
[NamedElement] => Array
(
[Element] => Array
(
[ElementType] => DEVICE
[id] => Device1ID
)
[name] => Device1
)
[hostName] => Device1.hostname
[ipAddress] => Device1.ip
[location] => location1
[modelName] =>
[modelNumber] =>
[parentID] => xxxYYY
[serialNumber] => 123456789
)
[1] => Array
(
[NamedElement] => Array
(
[Element] => Array
(
[ElementType] => DEVICE
[id] => Device2ID
)
[name] => Device2
)
[hostName] => Device2.hostname
[ipAddress] => Device2.ip
[location] => location1
[modelName] =>
[modelNumber] =>
[parentID] => xxxYYY
[serialNumber] => 987654321
)
)
...but I'd prefer:
Array
(
[0] => Array
(
[ElementType] => DEVICE
[id] => Device1ID
[name] => Device1
[hostName] => Device1.hostname
[ipAddress] => Device1.ip
[location] => location1
[modelName] =>
[modelNumber] =>
[parentID] => xxxYYY
[serialNumber] => 123456789
)
[1] => Array
(
[ElementType] => DEVICE
[id] => Device2ID
[name] => Device2
[hostName] => Device2.hostname
[ipAddress] => Device2.ip
[location] => location1
[modelName] =>
[modelNumber] =>
[parentID] => xxxYYY
[serialNumber] => 987654321
)
)
...and in the case of a single item being returned, this:
stdClass Object
(
[ArrayOfAlarm] => stdClass Object
(
[Alarm] => stdClass Object
(
[Element] => stdClass Object
(
[ElementType] => ALARM
[id] => Alarm1ID
)
[activeTime] =>
[AlarmSeverity] =>
[AlarmState] =>
[description] =>
[deviceID] =>
[recommendedAction] =>
[resolvedTime] =>
[sensorID] =>
)
)
)
...to this:
Array
(
[0] => Array
(
[Element] => Array
(
[ElementType] => ALARM
[id] => Alarm1ID
)
[activeTime] =>
[AlarmSeverity] =>
[AlarmState] =>
[description] =>
[deviceID] =>
[recommendedAction] =>
[resolvedTime] =>
[sensorID] =>
)
)
...but I'd prefer:
Array
(
[0] => Array
(
[ElementType] => ALARM
[id] => Alarm1ID
[activeTime] =>
[AlarmSeverity] =>
[AlarmState] =>
[description] =>
[deviceID] =>
[recommendedAction] =>
[resolvedTime] =>
[sensorID] =>
)
)

You can flatten a single of your items with the following function:
function flatten_item($array)
{
$result = [];
foreach ($array as $k => $v) {
if (is_array($v)) {
$result = array_merge($result, $this->flatten_item($v));
} else {
$result[$k] = $v;
}
}
return $result;
}
When you have an array of results, you can pass this function as the callback to array_map. Only the relevant portion of the code:
if (isset($v[0])) {
$this->response = array_map([$this, 'flatten_item'], $v);
return $this;
}
// Convert single result to an array
$this->response = [$this->flatten_item($v)];
return $this;
Since the response (so far) always has the same structure, you could extract the payload without using recursion, which allows you to also remove the foreach in the flatten function:
function flatten()
{
// Remove outer wrappers [ArrayOfX][X] by extracting the value
$payload = current(current($this->response));
if (isset($payload[0])) {
$this->response = array_map([$this, 'flatten_item'], $payload);
} else {
$this->response = [$this->flatten_item($payload)];
}
return $this;
}

The function found here does just what you want, with a slight modification (commented out concatination of parent keys): https://gist.github.com/kohnmd/11197713#gistcomment-1895523
function flattenWithKeys(array $array, $childPrefix = '.', $root = '', $result = array()) {
foreach($array as $k => $v) {
if(is_array($v) || is_object($v)) $result = flattenWithKeys( (array) $v, $childPrefix, $root . $k . $childPrefix, $result);
else $result[ /*$root .*/ $k ] = $v;
}
return $result;
}
Letting $object equal your provided object, use as follows:
$array = json_decode(json_encode($object), true);
$result =[];
foreach( $array['ArrayOfDevice']['Device'] as $key => $value ){
$result[$key] = flattenWithKeys($value);
}
print_r($result);
Output:
Array
(
[0] => Array
(
[ElementType] => DEVICE
[id] => Device1ID
[hostName] => Device1.hostname
[ipAddress] => Device1.ip
[location] => location1
[modelName] =>
[modelNumber] =>
[parentID] => xxxYYY
[serialNumber] => 123456789
)
[1] => Array
(
[ElementType] => DEVICE
[id] => Device2ID
[name] => Device2
[hostName] => Device2.hostname
[ipAddress] => Device2.ip
[location] => location2
[modelName] =>
[modelNumber] =>
[parentID] => xxxYYY
[serialNumber] => 987654321
)
)
See it run here: http://sandbox.onlinephpfunctions.com/code/851e93389b993a0e44c1e916291dc444f47047d3

Related

Add Array to Existing Object

This seems pretty easy, but I can't figure it out. I have an existing array of objects that was json_decoded. I need to iterate over those objects, append a new array to each, and then put the overall array back together. I can't get the new array to append in the correct place.
function appendToArray($arrayOfObjects) {
$newArray = array();
foreach ($arrayOfObjects as $object) {
echo "<pre>".print_r($object, true)."</pre>";
$newItems = array('item1', 'item2', 'item3');
$moreItems = array('more1', 'more2', 'more3');
$newObject = array();
$newObject["newItems"] = $newItems;
$newObject["moreItems"] = $moreItems;
$rebuiltObject = array();
array_push($rebuiltObject, $object);
$rebuiltObject['new_stuff'] = $newObject;
// $rebuiltObject[0]['new_stuff'] = $newObject;
array_push($newArray, $newObject);
}
return $newArray;
}
Here is an example of one of the objects that I start with (after json_decode of course:
0 => stdClass Object
(
[code] => some code
[first_name] => First
[last_name] => Last
[urls] => stdClass Object
(
[news] => http://www.somesite.com/news
[info] => http://www.somesite.com/info
)
[geo] => stdClass Object
(
[latitude] => 0.0
[longitude] => 0.0
[name] => building name
)
)
When this is done, what I want is this:
0 => stdClass Object
(
[code] => some code
[first_name] => First
[last_name] => Last
[urls] => stdClass Object
(
[news] => http://www.somesite.com/news
[info] => http://www.somesite.com/info
)
[geo] => stdClass Object
(
[latitude] => 0.0
[longitude] => 0.0
[name] => building name
)
[new_stuff] => Array
(
[newItems] => Array
(
[0] => item1
[1] => item2
[2] => item3
)
[moreItems] => Array
(
[0] => more1
[1] => more2
[2] => more3
)
)
)
But what I get is this:
[0] => stdClass Object
(
[code] => some code
[first_name] => First
[last_name] => Last
[urls] => stdClass Object
(
[news] => http://www.somesite.com/news
[info] => http://www.somesite.com/info
)
[geo] => stdClass Object
(
[latitude] => 0.0
[longitude] => 0.0
[name] => building name
)
),
[new_stuff] => Array
(
[newItems] => Array
(
[0] => item1
[1] => item2
[2] => item3
)
[moreItems] => Array
(
[0] => more1
[1] => more2
[2] => more3
)
)
)
Notice how [new_stuff] is outside of the original object. I need to get it inside. Everything else I've tried crashes and I'm totally out of ideas. Can anyone see how I can do this? Thank you!
You solution is one string:
$object->new_stuff = $newObject;
because every $object is object and not array. And adding new property to object is done via -> and not with [].
Full code:
function appendToArray($arrayOfObjects) {
foreach ($arrayOfObjects as $object) {
echo "<pre>".print_r($object, true)."</pre>";
$newItems = array('item1', 'item2', 'item3');
$moreItems = array('more1', 'more2', 'more3');
$newObject = array();
$newObject["newItems"] = $newItems;
$newObject["moreItems"] = $moreItems;
$object->new_stuff = $newObject;
}
return $arrayOfObjects;
}
You can just array_walk and change the object inplace:
array_walk($arrayOfObjects, function(&$item) {
$item->new_stuff = ["newItems" => ['item1', 'item2', 'item3'], etc];
});

echo json object $stdClass Object array

echo "<pre>"; print_r($data); echo "</pre>";
Gives following output:
$stdClass Object
(
[cartName] => AngularStore
[clearCart] =>
[checkoutParameters] => stdClass Object
(
)
[items] => Array
(
[0] => stdClass Object
(
[sku] => 01
[name] => Product 1
[price] => 600
[quantity] => 1
[stock] => 5
[scheme] => Array
(
[0] => stdClass Object
(
[name] => offerAB
[desc] => Description on the scheme
[no] => 3
[$$hashKey] => 01O
[checked] => 1
)
[1] => stdClass Object
(
[name] => offerXY
[desc] => Description on the scheme
[no] => 5
[$$hashKey] => 01P
)
[2] => stdClass Object
(
[name] => OfferPQ
[desc] => Description on the scheme
[no] => 2
[$$hashKey] => 01Q
[checked] => 1
)
[3] => stdClass Object
(
[name] => OfferLM
[desc] => Description on the scheme
[no] => 4
[$$hashKey] => 01R
)
)
[$$hashKey] => 05V
)
[1] => stdClass Object
(
[sku] => 02
[name] => Product 2
[price] => 500
[quantity] => 1
[stock] => 400
[scheme] => Array
(
[0] => stdClass Object
(
[name] => offerAB
[desc] => Description on the scheme
[no] => 6
[$$hashKey] => 01W
)
[1] => stdClass Object
(
[name] => offerXY
[desc] => Description on the scheme
[no] => 7
[$$hashKey] => 01X
)
[2] => stdClass Object
(
[name] => OfferPQ
[desc] => Description on the scheme
[no] => 3
[$$hashKey] => 01Y
)
[3] => stdClass Object
(
[name] => OfferLM
[desc] => Description on the scheme
[no] => 8
[$$hashKey] => 01Z
)
)
[$$hashKey] => 05W
)
)
[qty] => 3
)
I want to print value of sku , name, price using foreach loop
Since i m new to it i first started printing a single value
echo $data->items->arr[0]->sku;
Notice: Trying to get property of non-object getting this error
but i want to print the values in foreach please help!
Items is a property of the main object, and in itself is an array. This is what you're after:
foreach($data->items as $d) {
echo $d->name, '<br />', $d->sku, '<br />', $d->price;
}
If you want to access one of those element without a loop, you need to provide the array index, for example:
echo $data->items[0]->name
the easy way for you is convert object to array
function array2object($array) {
if (is_array($array)) {
$obj = new StdClass();
foreach ($array as $key => $val){
$obj->$key = $val;
}
}
else { $obj = $array; }
return $obj;
}
function object2array($object) {
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
}
else {
$array = $object;
}
return $array;
}
// example:
$array = array('foo' => 'bar', 'one' => 'two', 'three' => 'four');
$obj = array2object($array);
print $obj->one; // output's "two"
$arr = object2array($obj);
print $arr['foo']; // output's bar
foreach($data['items'] as $item) {
echo $item['sku'].PHP_EOL
echo $item['name'].PHP_EOL
echo $item['price'].PHP_EOL;
}

Extract a value from a data structure to a variable

I need help in extracting "Duration" from the data structure below, to a variable called var_dur.
The data comes from: print_r($data);
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[Job] => Array
(
[Arn] => arn:aws:elastictranscoder:us-west-2:98yufdos8u:job/fsdoiufds98u
[Id] => fdsu98sdufio
[Input] => Array
(
[AspectRatio] => auto
[Container] => auto
[FrameRate] => auto
[Interlaced] => auto
[Key] => iudyf98udsf
[Resolution] => auto
)
[Output] => Array
(
[AlbumArt] =>
[Composition] =>
[Duration] => 31
[Height] => 522
[Id] => 1
[Key] => dlsjf9ds8uf9d8sjuf9s.mp4
[PresetId] => sdufhy89dsfu98dsf
[Rotate] => 0
[SegmentDuration] =>
[Status] => Complete
[StatusDetail] =>
[ThumbnailPattern] => filename-700thumb-{resolution}-{count}
[Watermarks] => Array
(
)
[Width] => 640
)
[OutputKeyPrefix] =>
[Outputs] => Array
(
[0] => Array
(
[AlbumArt] =>
[Composition] =>
[Duration] => 31
[Height] => 522
[Id] => 1
[Key] => dlsjf9ds8uf9d8sjuf9s.mp4
[PresetId] => duisfy98dsuf89sd
[Rotate] => 0
[SegmentDuration] =>
[Status] => Complete
[StatusDetail] =>
[ThumbnailPattern] => filename-700thumb-{resolution}-{count}
[Watermarks] => Array
(
)
[Width] => 640
)
)
[PipelineId] => dsuf89dsuf89d
[Playlists] => Array
(
)
[Status] => Complete
)
)
)
Do something like this to iterate over the Array
function searchfor($text, $array) {
foreach (array_expression as $key => $val)
if($key == $text){
return $val
}
if(is_array($var){
return searchfor($text, $var);
}
}
return null;
}
Try it
$reflect = new ReflectionClass(OBJECT);
$props = $reflect->getProperties();
foreach ($props as $prop) {
print $prop->getName();
var_dump($prop->getValue());
}
About reflection class http://www.php.net/manual/en/reflectionclass.getproperties.php
About reflection property http://www.php.net/manual/en/class.reflectionproperty.php

Order array multi-dimensional

I have an array that looks like this:
$cars = array (
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'peugeot',
'b' => '306'),
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'audi',
'b' => 'a5'),
array(
'a' => 'peugeot',
'b' => '106'),
array(
'a' => 'peugeot',
'b' => '106'),
);
I need to order arrays like this to (id is the same as name):
name => audi
id=> audi
data => a4 => 2
a5 => 1
name => peugeot
id=> peugeot
data => 306 => 1
106 => 2
So the car brands need to be grouped an the car types counted.
I already have this code; but that is only for the group part and the count part is missing.
function mergeAndOrder($data){
// set group arrays
$i = 0; $group1 = array();
// loop trough array
$array = array(); $array2 = array();
if($data != null){
foreach($data AS $row){
// search and order level1
$search = array_search($row->a,$group1);
// this object is not found
if(is_int($search) == false){
$group1[$i] = $row->a;
$array[$i]['id'] = $row->a;
$array[$i]['name'] = $row->a;
$array[$i]['data'] = array();
$i++;
}
}
}
return $array;
}
Does somebody know an solution for this case? Thanks!
--- INPUT (part of) ---
a = lease company in this case
Array
(
[0] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[1] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[2] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[3] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
--- OUTPUT (part of) ---
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
[3] => Array
(
[id] => LPD
[name] => LPD
[data] => Array
(
)
)
)
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
If I understand your question correctly, this should do what you want.
function mergeAndOrder ($data) {
$output = array();
foreach ($data as $item) {
$id = $item->a;
$value = $item->b;
if (!array_key_exists($id, $output)) {
$output[$id] = array('id' => $id, 'name' => $id, 'data' => array());
}
if (!array_key_exists($value, $output[$id]['data'])) {
$output[$id]['data'][$value] = 0;
}
$output[$id]['data'][$value]++;
}
// Order by name element
uasort($output, function ($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
return $output;
}
Output:
Array
(
[audi] => Array
(
[id] => audi
[name] => audi
[data] => Array
(
[a4] => 2
[a5] => 1
)
)
[peugeot] => Array
(
[id] => peugeot
[name] => peugeot
[data] => Array
(
[306] => 1
[106] => 2
)
)
)

Find a value in nested associative array

I want to get the value of 'GUID' with the value of 'SamAccountName'. i.e. I only have the value pf 'SamAccountName' and I would like to get the value of 'GUID' for that part of the array.
Array
(
[0] => Array
(
[DistinguishedName] => CN=johnn#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 26d7c204-7db7-4601-8cd2-0dd0d3b37d97
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => johnn#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => John Nolan
[SamAccountName] => johnn_playgroundla
[FullSamAccountName] => EXCH024\johnn_playgroundla
[UserPrincipalName] => johnn#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[1] => Array
(
[DistinguishedName] => CN=csliney#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 71224be8-1b8b-46e7-97ef-2cd873bf9b7f
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => csliney#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Christopher Sliney
[SamAccountName] => csliney_playgroundla
[FullSamAccountName] => EXCH024\csliney_playgroundla
[UserPrincipalName] => csliney#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[2] => Array
(
[DistinguishedName] => CN=lee#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => b428b57f-4cd4-4243-a76a-f25f5ff3be97
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => lee#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => MSExchange2007Mailbox
[1] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Lee Roderick
[SamAccountName] => lee_playgroundla
[FullSamAccountName] => EXCH024\lee_playgroundla
[UserPrincipalName] => lee#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => MSExchangeMailboxes
[1] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[3] => Array
(
[DistinguishedName] => CN=theresa#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 4b2aee17-9e88-4de9-b95b-63a9877835a6
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => theresa#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Theresa Baker
[SamAccountName] => theresa_playgroundla
[FullSamAccountName] => EXCH024\theresa_playgroundla
[UserPrincipalName] => theresa#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
)
This was originally a stdClass object but I used json_decode(json_encode($obj), true) to convert to an associative array.
Sounds like you want to get the GUID portion for the value of 'SamAccountName'. Use a foreach loop:
function getGUID($san, $array) {
foreach($array as $a) {
if($a['SamAccountName'] == $san) {
return $a['GUID'];
}
}
return false;
}
$guid = getGUID("SamAccountNameHere", $yourArray);
You can use a simple loop to fetch it
$id = 0;
foreach($data as $item) {
if (isset($item['SamAccountName']) && 'accountName' == $item['SamAccountName']) {
$id = $item['GUID'];
break;
}
}
var_dump($id);
is this what you are looking for?
function findBySam($arrayList, $sam) {
foreach($arrayList as $array) {
if($array['SamAccountName'] == $sam) {
return $array;
}
}
return false;
}
Here is an example of a function that you could use. This assumes that there will be only one object with the SamAccountName that you supply in the array (it just uses the first one that it finds). It returns the GUID of the matching array and false if it cannot find an array with a matching SamAccountName.
function getGuidForSamAccountName($arr, $name) {
foreach ($arr as $elem) {
if ($elem['SamAccountName'] === $name) {
return $elem['GUID'];
}
}
return false; //No match found
}
You can use array_filter function of php:
http://php.net/manual/en/function.array-filter.php
example:
$GUID = "sample";
array_filter($array, "findElement");
function findElement($el) {
return $el["GUID"] == $_GLOBAL["GUID"];
}
Not a very elegant solution... but it should work.

Categories