I know variations of this question have been asked and answered before, but I'm a smart guy and after literally weeks of searching off and on, I cannot find what I need. There must be some easy way to do this I am missing.
Existing code:
$response = $sdb->get_attributes('domain','itemname');
$newresponse = $response->body->GetAttributesResult->to_array();
print_r($newresponse);
I get this result:
CFArray Object
(
[storage:ArrayObject:private] => Array
(
[Attribute] => Array
(
[0] => Array
(
[Name] => tenants
[Value] => Sam Smith
)
[1] => Array
(
[Name] => tenants
[Value] => Janet Jones
)
[2] => Array
(
[Name] => tenants
[Value] => Willy Wonka
)
[3] => Array
(
[Name] => buildingName
[Value] => 123 Main St.
)
)
)
)
What I want this data to look like:
Array
(
[tenants] => Array
(
[0] => Sam Smith
[1] => Janet Jones
[2] => Willy Wonka
)
[buildingName] => 123 Main St.
)
I need to account for [tenants]
having one or more values. The other variables may or may not have a single value. I also must account for the possibility that the [tenants]
may not be in sequential order (i.e. [buildingName]
was [2]
and another [tenants]
was in [3]
. There's clearly a concept here I do not understand. How do I do this?
EDIT to address private array comment
If I modify the existing code to the following:
$response = $sdb->get_attributes('domain','itemname');
$newresponse = $response->body->GetAttributesResult->to_array()->getArrayCopy();
print_r($newresponse);
I get this result:
Array
(
[Attribute] => Array
(
[0] => Array
(
[Name] => tenants
[Value] => Sam Smith
)
[1] => Array
(
[Name] => tenants
[Value] => Janet Jones
)
[2] => Array
(
[Name] => tenants
[Value] => Willy Wonka
)
[3] => Array
(
[Name] => companyname
[Value] => 123 Main St.
)
)
)
So while reference to 'private' is gone, the same basic question applies.
FINAL SOLUTION based on user2057272's answer
user2053727, you are both a gentleman (or gentlewoman) and a scholar. Your answer worked almost perfectly. The one issue I had was that the result was:
Array
(
[tenants] => Array
(
[0] => Sam Smith
[1] => Janet Jones
[2] => Willy Wonka
)
[buildingName] => Array
(
[0] => 123 Main St.
)
)
To fix the [buildingName]
to be a string instead of an array (because there is only one element), I modified my final code to the following:
$response= $sdb->get_attributes('domain','itemname');
$newresponse = $response->body->GetAttributesResult->to_array();
$new = array();
foreach($newresponse["Attribute"] as $key => $value)
{
if(!isset($new[$value['Name']]))
{
$new[$value['Name']] = array();
}
$new[$value['Name']][] = $value['Value'];
}
array_walk($new, $walker = function (&$value, $key) use (&$walker)
{
if (is_array($value))
{
if (count($value) === 1 && is_string($value[0]))
{
$value = $value[0];
}
else
{
array_walk($value, $walker);
}
}
}
);
print_r($new);
My gratitude to janmoesen (convert sub-arrays with only one value to a string) with the array_walk
part.
My result is now exactly what I need:
Array
(
[tenants] => Array
(
[0] => Sam Smith
[1] => Janet Jones
[2] => Willy Wonka
)
[buildingName] => 123 Main St.
)
Thank you.