Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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中传递模型返回的数组时出现问题_Php_Arrays_Codeigniter - Fatal编程技术网

Php 在Codeigniter中传递模型返回的数组时出现问题

Php 在Codeigniter中传递模型返回的数组时出现问题,php,arrays,codeigniter,Php,Arrays,Codeigniter,我在使用自定义库在Codeigniter 3中创建线程注释时遇到一些问题 当我在控制器中手动声明一个数组时,库正在工作,但是当我尝试从模型中传递一个数组时,它没有工作 使用编码到控制器中的数组的示例: $comments = array( array ( 'first_name' => 'James', 'last_name' => 'Smith', 'country' => 'UK', 'profile' => 'avatar87.jpg', '

我在使用自定义库在Codeigniter 3中创建线程注释时遇到一些问题

当我在控制器中手动声明一个数组时,库正在工作,但是当我尝试从模型中传递一个数组时,它没有工作

使用编码到控制器中的数组的示例:

$comments = array(  
array (
  'first_name' => 'James',
  'last_name' => 'Smith',
  'country' => 'UK',
  'profile' => 'avatar87.jpg',
  'id' => '1',
  'member_id' =>  '18',
  'policy_id' => '6',
  'comment' => '<p>This is my comment.Do you agree?</p>',
  'submitted' => '2015-09-22 07:20:21',
  'parent_id' => NULL),
   array (
  'first_name' => 'Peter',
  'last_name' => 'Green',
  'country' => 'Australia',
  'profile' => 'avatar88.jpg',
  'id' => '2',
  'member_id' =>  '18',
  'policy_id' => '6',
  'comment' => '<p>This is my comment.Do you agree?</p>',
  'submitted' => '2015-09-22 07:20:21',
  'parent_id' => '1'),
   array (
  'first_name' => 'Ollie',
  'last_name' => 'Ford',
  'country' => 'Australia',
  'profile' => 'avatar85.jpg',
  'id' => '3',
  'member_id' =>  '18',
  'policy_id' => '6',
  'comment' => '<p>This is my comment.Do you agree?</p>',
  'submitted' => '2015-09-22 07:20:21',
  'parent_id' => '1'),
         );

 $this->data['comments'] = $comments;
它不起作用

下面是从模型返回的$comments数组

 array (size=3)
  0 => 
    array (size=10)
      'first_name' => string 'Ollie' (length=5)
      'last_name' => string 'Falle' (length=5)
      'country' => string 'Australia' (length=9)
      'profile' => string 'avatar87.jpg' (length=12)
      'id' => string '1' (length=1)
      'member_id' => string '18' (length=2)
      'policy_id' => string '6' (length=1)
      'comment' => string '<p>This is my comment.Do you agree?</p>' (length=39)
      'submitted' => string '2015-09-22 07:20:21' (length=19)
      'parent_id' => string 'NULL' (length=4)
  1 => 
    array (size=10)
      'first_name' => string 'Ollie' (length=5)
      'last_name' => string 'Falle' (length=5)
      'country' => string 'Australia' (length=9)
      'profile' => string 'avatar87.jpg' (length=12)
      'id' => string '2' (length=1)
      'member_id' => string '18' (length=2)
      'policy_id' => string '6' (length=1)
      'comment' => string '<p>Blah blah blah</p>' (length=21)
      'submitted' => string '2015-09-22 15:00:00' (length=19)
      'parent_id' => string '1' (length=1)
  2 => 
    array (size=10)
      'first_name' => string 'Ollie' (length=5)
      'last_name' => string 'Falle' (length=5)
      'country' => string 'Australia' (length=9)
      'profile' => string 'avatar87.jpg' (length=12)
      'id' => string '3' (length=1)
      'member_id' => string '18' (length=2)
      'policy_id' => string '6' (length=1)
      'comment' => string '<p>Hello is it me your looking for.</p>' (length=39)
      'submitted' => string '2015-09-22 13:39:46' (length=19)
      'parent_id' => string '1' (length=1)
数组(大小=3)
0 => 
阵列(大小=10)
'first_name'=>字符串'Ollie'(长度=5)
'last_name'=>字符串'Falle'(长度=5)
'country'=>字符串'Australia'(长度=9)
'profile'=>string'avatar87.jpg'(长度=12)
'id'=>字符串'1'(长度=1)
'member_id'=>字符串'18'(长度=2)
'policy_id'=>字符串'6'(长度=1)
'comment'=>string'这是我的评论。你同意吗?

'(长度=39) “已提交”=>string“2015-09-22 07:20:21”(长度=19) 'parent_id'=>字符串'NULL'(长度=4) 1 => 阵列(大小=10) 'first_name'=>字符串'Ollie'(长度=5) 'last_name'=>字符串'Falle'(长度=5) 'country'=>字符串'Australia'(长度=9) 'profile'=>string'avatar87.jpg'(长度=12) 'id'=>字符串'2'(长度=1) 'member_id'=>字符串'18'(长度=2) 'policy_id'=>字符串'6'(长度=1) 'comment'=>string'Blah Blah Blah

'(长度=21) “已提交”=>string“2015-09-22 15:00:00”(长度=19) 'parent_id'=>字符串'1'(长度=1) 2 => 阵列(大小=10) 'first_name'=>字符串'Ollie'(长度=5) 'last_name'=>字符串'Falle'(长度=5) 'country'=>字符串'Australia'(长度=9) 'profile'=>string'avatar87.jpg'(长度=12) 'id'=>字符串'3'(长度=1) 'member_id'=>字符串'18'(长度=2) 'policy_id'=>字符串'6'(长度=1) 'comment'=>string'你好,是你在找我吗。

'(长度=39) “已提交”=>string“2015-09-22 13:39:46”(长度=19) 'parent_id'=>字符串'1'(长度=1)
我是不是在阵列上遗漏了什么

任何帮助都将不胜感激

作为参考,这里介绍了该库的使用方法

 <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Threaded
{

    public $parents  = array();
    public $children = array();

    /**
     * @param array $comments
     */
    public function arrange($comments)
    {
        foreach ($comments as $comment)
        {

            if ($comment['parent_id'] === NULL)
            {
                $this->parents[$comment['id']][] = $comment;
            }
            else
            {
                $this->children[$comment['parent_id']][] = $comment;
            }
        }
        $this->print_comments();
    }

    private function tabulate($depth)
    {
        for ($depth; $depth > 0; $depth--)
        {
           // echo "t";
        }
    }

    /**
     * @param array $comment
     * @param int $depth
     */
    private function format_comment($comment, $depth)
    {

        //echo "n";

        $this->tabulate($depth+1);

        echo "<li>";
        echo $comment['first_name'];
        echo $comment['last_name'];
        echo $comment['country'];
        echo $comment['profile'];
        echo $comment['member_id'];
        echo $comment['policy_id'];
        echo $comment['comment'];
        echo $comment['submitted'];
        echo $comment['id'];
        echo $comment['parent_id'];
        echo "</li>";
    }

    /**
     * @param array $comment
     * @param int $depth
     */
    private function print_parent($comment, $depth = 0)
    {
        $this->tabulate($depth);
        echo "<ul>";
        foreach ($comment as $c)
        {
            $this->format_comment($c, $depth);

            if (isset($this->children[$c['id']]))
            {
                $this->print_parent($this->children[$c['id']], $depth + 1);
            }
        }
        $this->tabulate($depth);
        echo "</ul>";
    }

    private function print_comments()
    {
        foreach ($this->parents as $c)
        {
            $this->print_parent($c);
        }
    }

}

不确定处理数据的逻辑是什么,但是(除非是打字错误)静态数组看起来像什么

array(data),  array(data),  array(data)  //with an unopened ) 
从模型返回的数组是

array(  array(data),  array(data),  array(data)  )

您试图对数组做什么?您是如何实现模型的输出的(即:var_dump();print_r())?在第一个示例中,您是如何定义$comments的?

您可以在一行中指定注释

$this->data['comments'] = $this->question_model->get_policy_comments($id);
只需通过下面的代码检查它,并查看数组的模式

echo '<pre>'; print_r($comments); die();
echo';打印(评论);模具();

所以现在阵列看起来是正确的。然而,我认为您的问题在于,在静态数组中,您实际上传递了一个NULL值,而在返回的数组中,您的“NULL”值实际上被解释为一个非NULL的字符串

在通过阵列之前:

public function arrange($comments)
{
    foreach ($comments as $comment)
    {

        if ($comment['parent_id'] === NULL || $comment['parent_id']==='NULL')
        {
            $this->parents[$comment['id']][] = $comment;
        }
        else
        {
            $this->children[$comment['parent_id']][] = $comment;
        }
    }
    $this->print_comments();
}

您好@doofficial,谢谢您的回复。线程库需要数组的格式与静态数组示例的格式相同。从模型返回的数组正在使用var_dump($comments)。在静态示例中,我只是通过$this->data['comments']=$comments传递数据;视图中有这个,你更新代码了吗?数组现在似乎是匹配的。正如@doofficial提到的,数组的结构似乎不同。你知道我该怎么配吗?
for($i=0; $i<count($comments); $i++){
    foreach($comments[$i] as $k => $v){
        if(strtoupper($v) === 'NULL'){
           $comments[$k] = NULL;
        }
    }
}
public function arrange($comments)
{
    foreach ($comments as $comment)
    {

        if ($comment['parent_id'] === NULL || $comment['parent_id']==='NULL')
        {
            $this->parents[$comment['id']][] = $comment;
        }
        else
        {
            $this->children[$comment['parent_id']][] = $comment;
        }
    }
    $this->print_comments();
}