Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array, stored in $array, that with

print "<pre>";
print_r($array);
print "</pre>";

gives an output like this:

Array
(
    [device] => Array
        (
            [0] => Array
                (
                    [@attributes] => Array
                        (
                            [name] => Low volt light
                            [id] => 10
                            [type] => Z-Wave Switch Multilevel
                            [value] => 0
                            [address] => 00016922-018
                            [code] => 
                            [canDim] => True
                            [lastChange] => 26-07-2014 17:31:33
                            [firstLocation] => Kitchen
                            [secondLocation] => Main
                        )

                )

            [1] => Array
                (
                    [@attributes] => Array
                        (
                            [name] => Light
                            [id] => 11
                            [type] => Z-Wave Switch Multilevel
                            [value] => 99
                            [address] => 00016922-019
                            [code] => 
                            [canDim] => True
                            [lastChange] => 31-07-2014 20:01:05
                            [firstLocation] => Bedroom
                            [secondLocation] => Main
                        )

                )

I cannot find my way to access/display for example the value (in this case 0) of device with [id]=>10. What syntax would be the right one in php?

share|improve this question
2  
I think your array is not correct. think of changing it based on your requirement, –  Pankaj Sharma 37 mins ago
    
seeing the @attributes key makes me think this originated as xml. If that is the case, you can load the xml in one of the php xml classes and use xpath to query for specific nodes by their attribute. –  Jonathan Kuhn 30 mins ago

4 Answers 4

There's not an easy way to do this, without looping through the array.

e.g.

foreach ($array['devices'] as $device) {
    if ($device['@attributes']['id'] === $idBeingSearchedFor) {
        // Do something with $device.
    }
}

Due to the @attributes array key, I'm guessing that this came from XML at some point: You might consider using Simple XML to parse it instead, as you could potentially use XPath then, which does support this type of access.

Alternatively again, you could reformat the array so it could be easily accessed by ID.

For example:

$formattedArray = array();

foreach ($array['devices'] as $device) {
    $id = $device['@attributes']['id'];
    $formattedArray[$id] = $device;
}

You could then access the device by its ID as follows:

$device = $formattedArray[$idBeingSearchedFor];
share|improve this answer
    
You don't even need to define the $id variable. :p –  Charlotte Dunois 22 mins ago

You could do it like that:

$id = 10;
$device = array();
foreach($array['device'] as $devices) {
    if($devices['@attributes']['id'] == $id) {
        $device = $devices['@attributes'];
        break;
    }
}

echo $device['value'];
share|improve this answer

Looks like SimpleXML, and if that is the case then those arrays are actually objects that, when put through print_r, look just like arrays. To access them, do the following:

Get straight to the data:

$name = $array->device[0]->attributes()->name;

Or loop through each of the attributes in the first device:

foreach ($array->device[0]->attributes() as $key => $value) {
    // Do something with the data. $key is the name of the
    // attribute, and then you have the $value.
}

Or you could loop through all the devices:

foreach ($array->device as $device) {
    foreach ($device->attributes() as $key => $value) {
        // Do something
    }
}
share|improve this answer
    
One of the reasons why I use var_dump() if I want to output something in order to see how it looks like. –  Charlotte Dunois 25 mins ago

It's simple ... try below...

print $array['device'][0]['@attributes']['id'];
or
print $array['device']['0']['@attributes']['id'];
share|improve this answer
    
This assumes that the index of the device is known, which is not what the question asked: The question states that the search is based on the id attribute of the device. –  ChrisC 25 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.