我正在尝试迭代具有以下结构的关联数组
var_export($ data)的输出
<?php
Harvest_Project::__set_state(array(
'_root' => 'project',
'_tasks' => array() ,
'_convert' => true,
'_values' => array(
'id' => '10122036',
'client-id' => '4861417',
'name' => 'ABC',
'code' => '',
'active' => 'true',
'billable' => 'true',
'bill-by' => 'Project',
'hourly-rate' => '145.0',
'budget' => '70.0',
'budget-by' => 'project',
'notify-when-over-budget' => 'true',
'over-budget-notification-percentage' => '80.0',
'over-budget-notified-at' => '2016-09-24',
'show-budget-to-all' => 'false',
'created-at' => '2016-03-15T21:38:40Z',
'updated-at' => '2016-05-31T23:19:58Z',
'starts-on' => '',
'ends-on' => '',
'estimate' => '70.0',
'estimate-by' => 'project',
'hint-earliest-record-at' => '2016-03-16',
'hint-latest-record-at' => '2016-08-11',
'notes' => '',
'cost-budget' => '',
'cost-budget-include-expenses' => 'false',
) ,
))
这是我写的迭代数组
的代码 <?php
$project_id=10122036;
$result=$api->getProject($project_id);
$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>";
?>
此代码仅创建列,并且由于某种原因不会为表中的每一行生成条目。因此,结果表是一个空表。 请建议我哪里出错了。
更新
此代码用于使用Harvest API查询Harvest。这是PHP Wrapper Library http://mdbitz.com/docs/harvest-api/,其中包含相关的类和方法。 getProject($ project id)是一种根据项目ID检索项目详细信息的方法。
答案 0 :(得分:1)
添加了html表格代码。
$project_id='10122036';
$result=$api->getProject($project_id);
echo "<table border='1'>
<tr><th>Project Name</th>
<th>Hourly Rate</th></tr>";
if( $result->isSuccess() ) {
$project = $result->data;?>
<tr><td><?php echo $project->get( "name" );?></td>
<td><?php echo $project->hourly-rate;?></td></tr>
<?php }else{?>
<tr><td colspan="2">No data from API</td></tr>
<?php } ?>
</table>
答案 1 :(得分:0)
由于您尝试使用api获取单个项目,请使用以下代码
$project_id='10122036';
$result=$api->getProject($project_id);
if( $result->isSuccess() ) {
$project = $result->data;
echo $project->get( "name" );
echo $project->hourly-rate;
}else{
echo "No data from API";
}
如果您看到输出&#34;没有来自API&#34;的数据,那么就没有从API返回的数据