Jump to content

Recommended Posts

I have an array which will be below. The array is being built from an XML file, the problem is I need to extrapolate data from it.

 

I am having trouble with looping through the array properly.

 

The output should be something like this:

 

Beth's state is 0   (Where Beth's is the name and 0 is the state)

Clint's state is 0

Array
(
    [0] => Array
        (
            [did] => 216616014153767704
            [known] => 1
            [lock] => 0
            [state] => 0
            [level] => 100
            [node] => 30
            [port] => 0
            [nodetype] => 16386
            [name] => Beth's
            [desc] => LED
            [colorid] => 1
            [type] => multilevel
            [rangemin] => 0
            [rangemax] => 99
            [power] => 0
            [poweravg] => 0
            [energy] => 0
            [score] => 0
            [productid] => 1
            [prodbrand] => TCP
            [prodmodel] => LED A19 11W
            [prodtype] => LED
            [prodtypeid] => 78
            [classid] => 2
            [class] => 
            [subclassid] => 1
            [subclass] => 
            [other] => 
        )

    [1] => Array
        (
            [did] => 216616014154116936
            [known] => 1
            [lock] => 0
            [state] => 0
            [level] => 100
            [node] => 30
            [port] => 0
            [nodetype] => 16386
            [name] => Clint's
            [desc] => LED
            [colorid] => 1
            [type] => multilevel
            [rangemin] => 0
            [rangemax] => 99
            [power] => 0
            [poweravg] => 0
            [energy] => 0
            [score] => 0
            [productid] => 1
            [prodbrand] => TCP
            [prodmodel] => LED A19 11W
            [prodtype] => LED
            [prodtypeid] => 78
            [classid] => 2
            [class] => 
            [subclassid] => 1
            [subclass] => 
            [other] => 
        )

)
Link to comment
https://forums.phpfreaks.com/topic/293816-multi-dimensional-array-issue/
Share on other sites

Sorry

$url            = "http://10.0.0.23/gwr/gop.php?cmd=gwrbatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>1234567890</token><fields>name,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd></gwrcmds>";
$onled          = '<img src="images\bulb-icon-on.png" />';
$offled         = '<img src="images\bulb-icon-off.png" />';
$xmlstring      = file_get_contents($url, false, $context);
$devices        = array();
$xml            = simplexml_load_string($xmlstring);

foreach($xml->gwrcmd->{'gdata'}->{'gip'}->{'room'}->{'device'} as $item){
        $device = array();

        foreach($item as $key => $value){
                $device[(string)$key] = (string)$value;
        }
        $devices[] = $device;
}

echo '<pre>'; print_r($devices); echo '</pre>';
echo "<br/><br/><hr/><br/>";
foreach ($devices as $info => $status) {
         foreach ($status as $statusnow){
             echo $statusnow.'<br />';
         }
     }

Here is also the XML from the url

<gwrcmds>
   <gwrcmd>
      <gcmd>RoomGetCarousel</gcmd>
      <gdata>
         <gip>
            <version>1</version>
            <rc>200</rc>
            <room>
               <rid>1</rid>
               <name>Master Bedroom</name>
               <desc />
               <known>1</known>
               <type>0</type>
               <color>00bd1f</color>
               <colorid>1</colorid>
               <img>img/room/green.png</img>
               <power>0</power>
               <poweravg>0</poweravg>
               <energy>0</energy>
               <device>
                  <did>216616014153767704</did>
                  <known>1</known>
                  <lock>0</lock>
                  <state>1</state>
                  <level>100</level>
                  <node>30</node>
                  <port>0</port>
                  <nodetype>16386</nodetype>
                  <name>Beth's</name>
                  <desc>LED</desc>
                  <colorid>1</colorid>
                  <type>multilevel</type>
                  <rangemin>0</rangemin>
                  <rangemax>99</rangemax>
                  <power>0.011</power>
                  <poweravg>0</poweravg>
                  <energy>0</energy>
                  <score>0</score>
                  <productid>1</productid>
                  <prodbrand>TCP</prodbrand>
                  <prodmodel>LED A19 11W</prodmodel>
                  <prodtype>LED</prodtype>
                  <prodtypeid>78</prodtypeid>
                  <classid>2</classid>
                  <class />
                  <subclassid>1</subclassid>
                  <subclass />
                  <other>
                     <rcgroup />
                     <manufacturer>TCP</manufacturer>
                     <capability>productinfo,identify,meter_power,switch_binary,switch_multilevel</capability>
                     <bulbpower>11</bulbpower>
                  </other>
               </device>
               <device>
                  <did>216616014154116936</did>
                  <known>1</known>
                  <lock>0</lock>
                  <state>0</state>
                  <level>100</level>
                  <node>30</node>
                  <port>0</port>
                  <nodetype>16386</nodetype>
                  <name>Clint</name>
                  <desc>LED</desc>
                  <colorid>1</colorid>
                  <type>multilevel</type>
                  <rangemin>0</rangemin>
                  <rangemax>99</rangemax>
                  <power>0</power>
                  <poweravg>0</poweravg>
                  <energy>0</energy>
                  <score>0</score>
                  <productid>1</productid>
                  <prodbrand>TCP</prodbrand>
                  <prodmodel>LED A19 11W</prodmodel>
                  <prodtype>LED</prodtype>
                  <prodtypeid>78</prodtypeid>
                  <classid>2</classid>
                  <class />
                  <subclassid>1</subclassid>
                  <subclass />
                  <other>
                     <rcgroup />
                     <manufacturer>TCP</manufacturer>
                     <capability>productinfo,identify,meter_power,switch_binary,switch_multilevel</capability>
                     <bulbpower>11</bulbpower>
                  </other>
               </device>
            </room>
         </gip>
      </gdata>
   </gwrcmd>
</gwrcmds>

I assume the array you posted is the contents of $data?

 

To get the name and state you loop over the $devices array and then grab the name and state items from the sub array. Example

foreach($devices as $device)
{
    echo $device['name'] . ' ' . $device['state'] . '<br />';
}

So ? You now have an array called $devices. Each element of that array is in itself an array that contains many items. So you begin a loop on $devices which gives you one element at a time and that element is itself an array, so you begin a loop on that array to get each key & value from its items. Just like the code that you built $devices with.

 

What do you want to do? Get Beth's state value?

foreach ($devices as $device)
{
if ($device['name']== 'Beth')
echo "Beth's state is ",$device['state']<br>";
else
if ($device['name'] == 'Clint')
echo "Clint's state is ",$device['state']<br>";
}

 

Something like that?

 

If 'name' is going to be the real key of all the data, then maybe you should assign the xml data to an element of $devices that uses the key of 'name' as the key for $devices. Then you could simply retrieve all of Beth's data by referencing $device['Beth'] in my above code, as $device['Beth']['state'];

 

or skip the array and use xpath. Try

$xml = simplexml_load_string($xmlstring); 
foreach ($xml->xpath('//device') as $dev) {
    echo "$dev->name state is $dev->state<br>";
}

 

This did exactly what I wanted in a much shorter concise code. was not familiar with xpath will have to read up on that one now.

 

This will also help me down the road when we add more leds to the system.

 

Thanks!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.