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条)

报告相同问题?

悬赏问题

  • ¥15 准备学习小程序搭建,谁能手把手的教我啊?
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e