duandiao3961
duandiao3961
2014-11-12 00:37
浏览 161
已采纳

foreach循环获取元素的值

I have been stuggling with this for the past couple hours, been trying so hard to get the value of userid and jobprocstatus out of this array.

this is the result of print_r($result);

 stdClass Object ( [accountid] => 6f3f55f6-67a1-11e4-a7c1-7e542e26bbf0 [userid] => 6f3f650b-67a1-11e4-a7c1-7e542e26bbf0 [cmd] => org.apache.cloudstack.api.command.admin.vm.DeployVMCmdByAdmin [jobstatus] => 1 [jobprocstatus] => 0 [jobresultcode] => 0 [jobresulttype] => object [jobresult] => stdClass Object ( [virtualmachine] => stdClass Object ( [id] => ccfbb592-ebfa-4f18-b861-4ae5e1a15426 [name] => VM-ccfbb592-ebfa-4f18-b861-4ae5e1a15426 [displayname] => VM-ccfbb592-ebfa-4f18-b861-4ae5e1a15426 [account] => admin [domainid] => ea3b19e6-67a0-11e4-a7c1-7e542e26bbf0 [domain] => ROOT [created] => 2014-11-11T17:39:31-0500 [state] => Running [haenable] => [zoneid] => 481b2bdf-90ba-4841-bd5a-f4fbf6027401 [zonename] => Toronto [hostid] => cc66aa5e-6be3-4f35-b538-87a9b09a6fc5 [hostname] => TOR-XENSRV61 [templateid] => ea41cab5-67a0-11e4-a7c1-7e542e26bbf0 [templatename] => CentOS 5.6(64-bit) no GUI (XenServer) [templatedisplaytext] => CentOS 5.6(64-bit) no GUI (XenServer) [passwordenabled] => [serviceofferingid] => 61ee4943-1af5-4168-9dee-fdd128fd58db [serviceofferingname] => Small Instance [cpunumber] => 1 [cpuspeed] => 500 [memory] => 512 [guestosid] => ea9ebb7a-67a0-11e4-a7c1-7e542e26bbf0 [rootdeviceid] => 0 [rootdevicetype] => ROOT [securitygroup] => Array ( ) [nic] => Array ( [0] => stdClass Object ( [id] => 1145aa85-cf95-4215-b1a7-439166698c23 [networkid] => fdeaaf3a-2ee1-45e3-bc15-b325b2ea517c [networkname] => test [netmask] => 255.255.255.0 [gateway] => 192.168.168.1 [ipaddress] => 192.168.168.177 [isolationuri] => vlan://286 [broadcasturi] => vlan://286 [traffictype] => Guest [type] => Isolated [isdefault] => 1 [macaddress] => 02:00:6d:ba:00:16 ) ) [hypervisor] => XenServer [instancename] => i-2-29-VM [tags] => Array ( ) [affinitygroup] => Array ( ) [displayvm] => 1 [isdynamicallyscalable] => 1 [ostypeid] => 142 [jobid] => 3a3c2e81-296b-4f00-906d-d4aac918487c [jobstatus] => 0 ) ) [created] => 2014-11-11T17:39:32-0500 [jobid] => 3a3c2e81-296b-4f00-906d-d4aac918487c ) 

this is what I have tried

<?php 

foreach ($result as $key => $value) {
    echo "$key : $value <br>";

}

output

accountid : 6f3f55f6-67a1-11e4-a7c1-7e542e26bbf0 
userid : 6f3f650b-67a1-11e4-a7c1-7e542e26bbf0 
cmd : org.apache.cloudstack.api.command.admin.vm.DeployVMCmdByAdmin 
jobstatus : 1 
jobprocstatus : 0 
jobresultcode : 0 
jobresulttype : object 

I'm simply trying to get the value of userid element out of this array, however everytime I try

echo "$result['userid']";

I get a bank screen,and I'm unable to identify the issue.

Your help is highly appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhi4830
    douzhi4830 2014-11-12 00:39
    已采纳

    This does the trick for a list of objects

    foreach ($result as $key => $value) {
        echo $value->userid;
    }
    

    For a single object:

    echo $value->userid;
    

    $value is an object, to get the property of an object you have to use the -> operator.

    If you want to get all the public properties of a list of objects:

    foreach ($result as $key => $obj) {
        foreach (get_object_vars($obj) as $name => $value) {
            echo "Object mapped to $key has property $name => $value";
        }    
    }
    
    点赞 评论
  • duanjue7508
    duanjue7508 2014-11-12 00:40

    With this you can access your userid:

    echo $result->userid;
    
    点赞 评论

相关推荐