我知道很多帖子都有相同的标题,不过,我的问题已经部分修复了。
打印阵列:
$data['posts'] = $this->model_mutamba->getPosts($email);
print_r('<pre>');
print_r( $data['posts']);
print_r('<pre>');
数组:
Array
(
[0] => Array
(
[0] => stdClass Object
(
[post_id] => 5
[aluno_id] => 3
[post_text] => Hello my name is Joana!
[post_image] =>
[post_video] =>
[post_comments] =>
[post_likes] => 0
[post_flags] => 0
[post_date] => 2014-12-11 03:03:33
)
)
[1] => Array
(
[0] => stdClass Object
(
[post_id] => 3
[aluno_id] => 1
[post_text] => Mestre zeca na area!
[post_image] =>
[post_video] =>
[post_comments] => 0
[post_likes] => 0
[post_flags] => 0
[post_date] => 2014-12-10 19:37:16
)
[1] => stdClass Object
(
[post_id] => 4
[aluno_id] => 1
[post_text] => ulalalala
[post_image] =>
[post_video] =>
[post_comments] => 0
[post_likes] => 0
[post_flags] => 0
[post_date] => 2014-12-10 21:00:29
)
)
[2] => Array
(
[0] => stdClass Object
(
[post_id] => 1
[aluno_id] => 10
[post_text] => olaaa a todossss
[post_image] =>
[post_video] =>
[post_comments] => 0
[post_likes] => 0
[post_flags] => 0
[post_date] => 2014-12-10 05:18:11
)
[1] => stdClass Object
(
[post_id] => 2
[aluno_id] => 10
[post_text] => mais umas vez estamos aqui e brindar.
[post_image] =>
[post_video] =>
[post_comments] => 0
[post_likes] => 0
[post_flags] => 0
[post_date] => 2014-12-10 15:17:26
)
)
)
我在CodeIgniter中的项目,我试图将其循环:
if ("" != $posts) {
foreach ($posts as $object) {
$p = $object[0];
echo "<br>" . $p->post_text;
}
}
它显示帖子中的文字,但不是全部。它仅显示[0]索引中的项目。我也尝试过这样的事情:
$i = -1;
foreach ($posts as $object) {
$i++;
$p = $object[$i];
echo "<br>" . $p->post_text;
}
但是得到&#34; Undefined offset 2&#34;。
我知道这一定是我想念的简单事。 任何帮助将不胜感激。
答案 0 :(得分:2)
你有一个数组,其中每个元素都包含一个对象数组。
$data['posts'] = $this->model_mutamba->getPosts($email);
// just being really explicit to make sure $data['posts'] is a non empty array
if (is_array($data['posts']) && count($data['posts']) > 0) {
foreach ($data['posts'] as $array_of_objects) {
foreach ($array_of_objects as $object) {
echo "<br>" . $object->post_text;
}
}
}
答案 1 :(得分:0)
我使用了@scrowler建议的另一个循环。
$i= 0;
foreach($posts as $object){
$i++;
foreach($object as $i=>$p ){
echo "<br>". $p->post_text;
}
}
像魅力一样工作。感谢大家的敬意;)