mousey3209 mousey3209 - 1 month ago 5
PHP Question

Extracting a property from an array of an array of objects

I've got an object, containing an array of objects, containing an array of values:

stdClass Object (
[devices] => Array (
[0] => stdClass Object (
[location] => 1
[delegate] =>
[type] => 1
[id] => 1234
[IP] => 1.2.3.4
[name] => host1
[owner] => user6
[security] => 15
)
[1] => stdClass Object (
[location] => 2
[delegate] =>
[type] => 1
[id] => 4321
[managementIP] => 4.3.2.1
[name] => host2
[owner] => user9
[security] => 15
)
)
)


I want to extract just the id and name into an array in the form of:
$devices['id'] = $name;

I considered using the
array_map()
function, but couldn't work out how to use it... Any ideas?

Answer

This will generate you a new array like I think you want

I know you says that delegate is an object but the print does not show it that way

$new = array();
foreach($obj->devices as $device) {
    $new[][$device->id] = $device->name;
}