我有阵列关联,数组名称是$ 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'];
}
请帮帮我..
感谢...
答案 0 :(得分: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']);
}
}
根据需要调整变量名称。祝你好运,〜雷
答案 1 :(得分:0)
这似乎测试出来了。我将属性更改为“public”,这样我就可以避免编写另一个类了。移动部件从第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']);
}