我正在尝试让我的数组显示此数组中的一些元素。
<?php
$data = array(
1000 => array( "id" => 1000, "number" => 2380, "name" => "CS2",
"instructor" => "Chen", "rating" => null ),
1001 => array( "id" => 1001, "number" => 2333, "name" => "Computer Organization and Assembly",
"instructor" => "Reilly", "rating" => null ),
1002 => array( "id" => 1002, "number" => 3342, "name" => "Awesome",
"instructor" => "Tomai", "rating" => null )
);
$ratings = array(
4 => "Excellent",
3 => "Good",
2 => "Drop",
1 => "F"
)
?>
我已将此代码设置为显示元素,现在它只是一个真正没有做太多的准系统,但至少我希望它能显示我在这里的内容,但它一直告诉我它无法显示数组。
错误详情:
Notice: Trying to get property of non-object in C:\xampp\htdocs\phptiem\courses.php on line 29
Notice: Trying to get property of non-object in C:\xampp\htdocs\phptiem\courses.php on line 30
Notice: Trying to get property of non-object in C:\xampp\htdocs\phptiem\courses.php on line 31
注意:为了我的娱乐,我故意拼错了phptiem
。
这是我到目前为止的代码似乎并没有真正起作用。
我的服务器上也没有短打开标签,因为它不起作用所以我被迫使用<?php ... ?>
。
<!DOCTYPE html>
<?php
require_once( "data.php" );
?>
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Course Listings</h1>
</div>
<table class="table">
<?php
print_r($data);
foreach( $data as $key => $value )
{
?>
<tr>
<td><a href="course.php"> <?php $value->number ?> </a></td>
<td><?php $value->name ?> </td>
<td><?php $value->instructor ?> </td>
</tr>
<?php
};
?>
</table>
</div>
<script src="http://code.jquery.com/jquery.min.js"></script>
</body>
</html>
答案 0 :(得分:1)
您使用$ value作为对象。但它确实是一个阵列。您应该将其打印为$ value [&#39; number&#39;]或将其转换为stdClass。
提及打印。您在<td><a href="course.php"> <?php $value->number ?> </a></td>
和其他地方都缺少echo命令。