Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter中查看来自foreach的数据_Php_Codeigniter_Foreach - Fatal编程技术网

Php 在codeigniter中查看来自foreach的数据

Php 在codeigniter中查看来自foreach的数据,php,codeigniter,foreach,Php,Codeigniter,Foreach,首先请原谅,我是codeigniter框架的初学者。我想在视图中显示父数组的所有子内容。我假设我的数据检索部分已经完成,现在我需要知道如何使用foreach获取值。我使用了foreach,但出现了错误(非法的字符串偏移量)。下面是我在页面上获得的var_dump值 array(1) { ["post"]=> array(6) { ["post_id"]=> string(2) "52" ["status"]=> string(29) "T

首先请原谅,我是codeigniter框架的初学者。我想在视图中显示父数组的所有子内容。我假设我的数据检索部分已经完成,现在我需要知道如何使用foreach获取值。我使用了foreach,但出现了错误(非法的字符串偏移量)。下面是我在页面上获得的var_dump值

array(1) {
  ["post"]=>
  array(6) {
    ["post_id"]=>
    string(2) "52"
    ["status"]=>
    string(29) "This is a test status update."
    ["user"]=>
    string(1) "1"
    ["time"]=>
    string(19) "2015-02-05 19:47:42"
    ["modified"]=>
    string(19) "0000-00-00 00:00:00"
    ["comment"]=>
    array(2) {
      [0]=>
      array(3) {
        ["comment_id"]=>
        string(1) "3"
        ["comment"]=>
        string(22) "This is a test comment"
        ["comment_datetime"]=>
        string(19) "2015-02-06 08:36:15"
      }
      [1]=>
      array(3) {
        ["comment_id"]=>
        string(1) "5"
        ["comment"]=>
        string(11) "sdfsdfsdfds"
        ["comment_datetime"]=>
        string(19) "2015-02-06 09:33:25"
      }
    }
  }
}
我尝试过获取如下数据:

<?php
    foreach($post as $data){
     $data['status'];
     $data['post_id'];
    }
?>

但是,当我这样做时,会收到非法的字符串偏移量错误消息。

您应该使用

foreach ($post['post'] as $data) 

如果您正在打印的数组名为$post,您可以看到这是一个嵌套数组。

如果您仔细查看数据

 $data['post_id'];  
不在数组中,它在$data['post']中的数据中,数组中有数组。。因此,您需要相应地查找数据

要访问代码中的键和值,可以使用

  foreach($post as $key=>$value){
  // $key will have the keys and $value will have the corresponding values 
  }
试试这个代码

<?php
    foreach($post as $data){
     $data['post']['status'];
     $data['post']['post_id'];
    }
?>


因为posts数组是关联数组,所以最好在foreach中使用$posts['post']。这是我共享的链接,用于在将数据传递到视图put current($dataset)并将其传递到视图之前解决问题。因此,您可以通过$post['COMMENTS']访问它

$this->load->view('your_view_name',current($data_set_passing));
试试这个

<?php
    foreach($post->result_array() as $data){
     $data['status'];
     $data['post_id'];
    }
?>

试试这个,你会得到你想要的结果:

foreach($posts as $data)
    {

        echo $data['post_id'];
        echo $data['status'];
        echo $data['user'];
        echo $data['time'];
        echo $data['modified'];
        foreach($data['comment'] as $sub)
            {
                echo $sub['comment_id'];
                echo $sub['comment'];
                echo $sub['comment_datetime'];
            }
    }

尝试打印foreach循环中的值如何定义$key和$value?