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

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 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";
        }    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题