-1

我有数组关联,数组名称是 $monitorings。我试图通过使用函数 print_r 查看一个数组值,输出是这样的。

print_r($monitorings);
 //output
 master_monitoring_collection Object ( [position:protected] => 0 [storage:protected] =>   
 Array ( [0] => master_monitoring Object ( [table:protected] => master_monitoring     
 [values:protected] => Array ( [prvd_name] => Axis [prvd_status] => Y [prvd_lastprob]   
 =>[prvd_prob] => [prvd_traffic_sms] => [update_date] => 2011-11-26 22:54:39 ) ) [1] =>   
 master_monitoring Object ( [table:protected] => master_monitoring [values:protected]=> 
 Array ( [prvd_name] => Esia [prvd_status] => Y [prvd_lastprob] => [prvd_prob] => 
 [prvd_traffic_sms] => [update_date] => 2011-11-26 22:54:07 ) )

问题是我这样尝试时无法获得价值。

foreach($monitorings as $monitoring){
   echo $monitoring['prvd_name'];
}

请帮我..

谢谢...

4

2 回答 2

0

这似乎测试正常。我将属性更改为“公共”,这样我就可以避免编写另一个类。移动部件从第 80 行开始。

http://www.laprbass.com/RAY_temp_casper.php

<?php // RAY_temp_casper.php
error_reporting(E_ALL);
echo '<pre>';

/* STRUCTURE WE ARE TRYING TO CREATE
master_monitoring_collection Object (
   [position:protected] => 0
   [storage:protected] => Array (
      [0] => master_monitoring Object (
         [table:protected] => master_monitoring
         [values:protected] => Array (
            [prvd_name] => Axis
            [prvd_status] => Y
            [prvd_lastprob] =>
            [prvd_prob] =>
            [prvd_traffic_sms] =>
            [update_date] => 2011-11-26 22:54:39 )
         )
      [1] => master_monitoring Object (
         [table:protected] => master_monitoring
         [values:protected]=> Array (
            [prvd_name] => Esia
            [prvd_status] => Y
            [prvd_lastprob] =>
            [prvd_prob] =>
            [prvd_traffic_sms] =>
            [update_date] => 2011-11-26 22:54:07
         )
      )
*/ // END OF STRUCTURE

Class Master_monitoring
{
    public $table, $values;
    public function __construct($values)
    {
        $this->table = 'master_monitoring';
        $this->values = $values;
    }
}

Class Master_monitoring_collection
{
    public $position, $storage;
    public function __construct($array)
    {
        $this->position = 0;
        $this->storage = $array;
    }
}


$a = array
( 'prvd_name'        => 'Axis'
, 'prvd_status'      => 'Y'
, 'prvd_lastprob'    => NULL
, 'prvd_prob'        => NULL
, 'prvd_traffic_sms' => NULL
, 'update_date'      => '2011-11-26 22:54:39'
)
;
$b = array
( 'prvd_name'        => 'Esia'
, 'prvd_status'      => 'Y'
, 'prvd_lastprob'    => NULL
, 'prvd_prob'        => NULL
, 'prvd_traffic_sms' => NULL
, 'update_date'      => '2011-11-26 22:54:07'
)
;

$aobj = new Master_Monitoring($a);
$bobj = new Master_Monitoring($b);
$mobj = array($aobj, $bobj);
$monitors = new Master_monitoring_collection($mobj);

// DOES IT LOOK RIGHT? (YES)
print_r($monitors);

// SHOW HOW TO USE THE ITERATOR
foreach ($monitors->storage as $mmo)
{
    var_dump($mmo->values['prvd_name']);
}
于 2012-12-23T18:21:28.517 回答
0

如果你做两件事,你的输出会更容易阅读。首先,在打印之前回显预格式化标签,其次,考虑使用 var_dump() 而不是 print_r() 因为 var_dump() 将为您提供数据类型特征以及可视化内容。

看来您有这样的结构。

//output
master_monitoring_collection Object ( 
   [position:protected] => 0 
   [storage:protected] => Array ( 
      [0] => master_monitoring Object (  
         [table:protected] => master_monitoring     
         [values:protected] => Array (  
            [prvd_name] => Axis 
            [prvd_status] => Y 
            [prvd_lastprob] => 
            [prvd_prob] => 
            [prvd_traffic_sms] => 
            [update_date] => 2011-11-26 22:54:39 ) 
         ) 
      [1] => master_monitoring Object ( 
         [table:protected] => master_monitoring 
         [values:protected]=> Array ( 
            [prvd_name] => Esia 
            [prvd_status] => Y 
            [prvd_lastprob] => 
            [prvd_prob] => 
            [prvd_traffic_sms] => 
            [update_date] => 2011-11-26 22:54:07 
         ) 
      )

您可能无法获取这些值,因为它们受到保护。你的 foreach() 指令的上下文很重要。它必须在类内部或类的扩展中才能访问受保护的对象。如果它位于可以看到受保护属性的上下文中,则您的迭代器可能如下所示:

foreach ($master_monitoring_collection->storage as $storage)
{
    foreach ($storage as $master_monitoring)
    {
        var_dump($master_monitoring->values['prvd_name']);
    }
}

根据需要调整变量名称。祝你好运,~雷

于 2012-12-23T14:42:31.957 回答