dongliang1996 2014-05-20 17:36
浏览 78
已采纳

PHP从嵌套数组中提取值并将其放置在循环中以回显值

I'm trying to extract the virtual machine names that are being pulled from a vcenter server via the below SOAP requests. Whilst I'm getting the results (see the var_dump below), I can't figure out how to put a loop in to get the names as there are many embedded arrays and everything I try either returns an error or Array. I appreciate this is probably quite a simple one but i just can't figure it out.

It's the name value in the code extract below i'm trying to get.

$request->specSet = array (
                               'propSet' => array (
        array ('type' => 'VirtualMachine', 'all' => 0, 'pathSet' => array('name','runtime.powerState', 'config.hardware.numCPU', 'config.hardware.memoryMB')),
    ),

PHP Page

$ss1 = new soapvar(array ('name' => 'FolderTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$ss2 = new soapvar(array ('name' => 'DataCenterVMTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$a = array ('name' => 'FolderTraversalSpec', 'type' => 'Folder', 'path' => 'childEntity', 'skip' => false, $ss1, $ss2);
$ss = new soapvar(array ('name' => 'FolderTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$b = array ('name' => 'DataCenterVMTraversalSpec', 'type' => 'Datacenter', 'path' => 'vmFolder', 'skip' => false, $ss);
$res = null;
try
  {
    $request = new stdClass();
    $request->_this = $ret->propertyCollector;
    $request->specSet = array (
                               'propSet' => array (
        array ('type' => 'VirtualMachine', 'all' => 0, 'pathSet' => array('name','runtime.powerState', 'config.hardware.numCPU', 'config.hardware.memoryMB')),
    ),
    'objectSet' => array (
        'obj' => $ret->rootFolder,
        'skip' => false,
        'selectSet' => array (
            new soapvar($a, SOAP_ENC_OBJECT, 'TraversalSpec'),
            new soapvar($b, SOAP_ENC_OBJECT, 'TraversalSpec'),
            ),
        )
    );
     $res = $client->__soapCall('RetrieveProperties', array((array)$request));
   } 
catch (Exception $e)
    {
        echo $e->getMessage();
    }

Var_dump()

object(stdClass)#46 (1) {
  ["returnval"]=>
  array(2) {
    [0]=>
    object(stdClass)#47 (2) {
      ["obj"]=>
      object(stdClass)#48 (2) {
        ["_"]=>
    string(5) "vm-35"
    ["type"]=>
    string(14) "VirtualMachine"
  }
  ["propSet"]=>
  array(4) {
    [0]=>
    object(stdClass)#49 (2) {
      ["name"]=>
      string(24) "config.hardware.memoryMB"
      ["val"]=>
      int(128)
    }
    [1]=>
    object(stdClass)#50 (2) {
      ["name"]=>
      string(22) "config.hardware.numCPU"
      ["val"]=>
      int(1)
    }
    [2]=>
    object(stdClass)#51 (2) {
      ["name"]=>
      string(4) "name"
      ["val"]=>
      string(7) "test123"
    }
    [3]=>
    object(stdClass)#52 (2) {
      ["name"]=>
      string(18) "runtime.powerState"
      ["val"]=>
      string(10) "poweredOff"
    }
  }
}
[1]=>
object(stdClass)#53 (2) {
  ["obj"]=>
  object(stdClass)#54 (2) {
    ["_"]=>
    string(5) "vm-36"
    ["type"]=>
    string(14) "VirtualMachine"
  }
  ["propSet"]=>
  array(4) {
    [0]=>
    object(stdClass)#55 (2) {
      ["name"]=>
      string(24) "config.hardware.memoryMB"
      ["val"]=>
      int(128)
    }
    [1]=>
    object(stdClass)#56 (2) {
      ["name"]=>
      string(22) "config.hardware.numCPU"
      ["val"]=>
      int(1)
    }
    [2]=>
    object(stdClass)#57 (2) {
      ["name"]=>
      string(4) "name"
      ["val"]=>
      string(7) "test456"
    }
    [3]=>
    object(stdClass)#58 (2) {
      ["name"]=>
      string(18) "runtime.powerState"
      ["val"]=>
      string(10) "poweredOff"
        }
      }
    }
  }
}
  • 写回答

1条回答 默认 最新

  • doushansu9012 2014-05-21 09:11
    关注

    I knew it was a relatively simple fix. I added the below to the code to get the required values out.

    $tvCPU = 0;
    $tvRAM = 0;
    
    echo "<table><tr><td width='450px'>Name</td><td width='100px'>vRAM (MB)</td><td         width='100px'>vCPU</td><td width='100px'>Power State</td></tr>";
    $arrlength=count($res ->returnval);
    for($x=0;$x<$arrlength;$x++) {
    $name = $res -> returnval[$x] -> propSet[2]->val;
    $vRam = $res -> returnval[$x] -> propSet[0]->val;
    $vCPU = $res -> returnval[$x] -> propSet[1]->val;
    $pState = $res -> returnval[$x] -> propSet[3]->val;
    
    echo "<tr><td width='450px'>".$name."</td><td>".$vRam."</td><td>".$vCPU."</td><td>".$pState."</td>    </tr>";
    $tvCPU = $tvCPU + $vCPU; 
    $tvRAM = $tvRAM + $vRam; 
    }
    echo "<tr><td width='450px'>Total VMs - ".$x."</td>    <td>". $tvRAM."</td>   <td>". $tvCPU."</td>   </tr>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程