Chad
Chad

Reputation: 103

Pull array results from var_dump in PHP

I have an AXL query which returns the names of device(s). When I try and display this information in my webpage it is outputting more info than needed. Here is the current code:

$payload = array(
    "userid" => "$_POST[iduser]",
    "returnedTags" => array(
        "associatedDevices" => "",
        "device" => ""
    )
);

$response = $client->getUser($payload);
print_r ($response);

Output:

stdClass Object ( 
  [return] => stdClass Object ( 
    [user] => stdClass Object ( 
      [associatedDevices] => stdClass Object ( 
        [device] => Array ( 
          [0] => SEP5065F3B9AB95 
          [1] => SEPB00CD1D0047D 
        ) 
      ) 
      [uuid] => {29C4300A-AC63-E9C6-F240-CDF7F0DBF153} 
    ) 
  ) 
)

I am only needing the entries of the devices starting with "SEP".

Additionally, when I hard code the array I get the data I am looking for, however this is not acceptable as there may be more or less than 2 results:

echo '<b>MAC Address : </b>'; 
echo $array['return']['user']['associatedDevices']['device'][0];
echo '<br><br>';
echo '<b>MAC Address : </b>'; 
echo $array['return']['user']['associatedDevices']['device'][1];
echo '<br><br>';

Any help on this would be greatly welcome. I also looked into using a foreach but was unable to return results of that as well.

Thanks!

Upvotes: 0

Views: 283

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 79024

You didn't show your loop attempt, but with those objects and array:

foreach($response->return->user->associatedDevices->device as $device) {
    echo $device . '<br><br>';
}

Or if it is this simple, then join them with the <br>:

echo implode('<br><br>', $response->return->user->associatedDevices->device);

Upvotes: 4

Related Questions