将关联数组转换为表

时间:2016-04-15 17:01:42

标签: php html arrays

我正在尝试以HTML表格的形式转换关联数组。这就是Array的样子

Array ( [] => Harvest_Project Object ( 
               [_root:protected] => project 
               [_tasks:protected] => Array ( ) 
               [_convert:protected] => 1 
               [_values:protected] => Array ( 
                     [id] => \ 
                     [client-id] => - 
                     [name] => Internal 
                     [code] => 
                     [active] => false 
                     [billable] => true 
                     [bill-by] => none 
                     [hourly-rate]=>-

这是我用PHP编写的用于将数组转换为表的代码

<?php
require_once(dirname(__FILE__) . '/HarvestAPI.php');

/* Register Auto Loader */
spl_autoload_register(array('HarvestAPI', 'autoload'));

$api = new HarvestAPI();
$api->setUser( $user );
$api->setPassword( $password );
$api->setAccount( $account );

$api->setRetryMode( HarvestAPI::RETRY );
$api->setSSL(true);

$result = $api->getProjects();
$data = $result->get( "data" );
echo "<table border='1'>
    <tr><td>Project Name</td>
    <td>Hourly Rate</td>
    </tr>";
        foreach($data as $key=>$fruit) {
                        ?>
                     <tr><td><?php echo $fruit["Name"];?></td>
                     <td><?php echo $fruit["hourly-rate"];?></td></tr>
        <?php ;}
    echo "</table>";
?>

我想展示一个有2列的表格 - 项目名称和每小时费率

但是一旦我执行代码就会抛出错误 致命错误:不能将Harvest_Project类型的对象用作第24行的数组。它指的是这一行

<tr><td><?php echo $fruit["Name"];?></td>

请建议如何去做。

2 个答案:

答案 0 :(得分:2)

您无法以相同的方式访问对象和数组。请改用 <tr><td><?php echo $fruit->name;?></td> <td><?php echo $fruit->{'hourly-rate'};?></td></tr> 运算符:

data{
    int nObs;
    int S[nObs];
    int<lower=0> n[nObs];
    real Area2[nObs];
    real Area3[nObs];
    real Julian_Day[nObs];
    int Year[nObs];
    int nYears;
}

parameters{
    real intercept_raw;
    real beta_Area2_raw;
    real beta_Area3_raw;
    real gamm_raw;
    real gamm_raw_Area2;
    real gamm_raw_Area3;
    real vary_Year[nYears];
    real<lower=0> sigma_Year;
}

transformed parameters {
    real intercept;
    real beta_Area2;
    real beta_Area3;
    real gamm;
    real gamm_Area2;
    real gamm_Area3;
    intercept <- intercept_raw*20;
    beta_Area2 <- beta_Area2_raw*5;
    beta_Area3 <- beta_Area3_raw*5;
    gamm <- gamm_raw*5;
    gamm_Area2 <- gamm_raw_Area2*5;
    gamm_Area3 <- gamm_raw_Area3*5;
}

model{
    real vary[nObs];
    real y[nObs];
    // Priors
    intercept_raw ~ normal(0,1);
    beta_Area2_raw ~ normal( 0 , 1 );
    beta_Area3_raw ~ normal( 0 , 1 );
    gamm_raw ~ normal( 0 , 1 );
    gamm_raw_Area2 ~ normal( 0 , 1 );
    gamm_raw_Area3 ~ normal( 0 , 1 );
    sigma_Year ~ cauchy( 0 , 5 );
    // random effect
    for ( j in 1:nYears ) vary_Year[j] ~ normal( 0 , sigma_Year );
    // Fixed effects
    for ( i in 1:nObs ) {
        vary[i] <- vary_Year[Year[i]];
        y[i] <- vary[i] + intercept
                + beta_Area2 * Area2[i]
                + beta_Area3 * Area3[i]
                + gamm * Julian_Day[i]
                + gamm_Area2 * Area2[i] * Julian_Day[i]
                + gamm_Area3 * Area3[i] * Julian_Day[i];
    }
     S ~ binomial_logit( n, y );
}

generated quantities{
  real y_pred[nObs];
  real dev;
  real y[nObs];
  real vary[nObs];
  vector[nObs] log_lik;
  dev <- 0;
    for ( i in 1:nObs ) {
       vary[i] <- vary_Year[Year[i]];
       y[i] <- vary[i] + intercept
                + beta_Area2 * Area2[i]
                + beta_Area3 * Area3[i]
                + gamm * Julian_Day[i]
                + gamm_Area2 * Area2[i] * Julian_Day[i]
                + gamm_Area3 * Area3[i] * Julian_Day[i];
        log_lik[i] <- binomial_log( S[i] , n[i] , inv_logit(y[i]));       
        dev <- dev + (-2) * log_lik[i];
        y_pred[i] <- binomial_rng(100, inv_logit(y[i]) );
    }
}

您需要在每小时费率周围使用大括号,因为变量不能在名称中包含破折号。相反,它们被视为减法运算符。

答案 1 :(得分:0)

$fruit是一个包含许多键的对象。要获取对象的name键的_values键,可以在foreach循环中执行此操作:

$values = $fruit-> _values
$values['name']; // retrieve the name "Internal"