【问题标题】:Array to string conversion cakephp数组到字符串的转换 cakephp
【发布时间】:2014-02-20 14:12:30
【问题描述】:

我正在开发 Cake PHP。请给我一个解决方案来解决这个问题:

Controller.php:

public function test() {
    $this->set('users_list', $this->User->find('list', array('fields' => array('User.id', 'User.fullname'))));
}

test.ctp:

<?php echo $users_list ?>

错误:

抛出错误:数组到字符串的转换 php

我需要显示这些值。

【问题讨论】:

  • 显示你使用users_list的代码。即您的视图文件代码正是错误所在的位置。
  • 大概你想做&lt;?php foreach($users_list as $user){ echo $user-&gt;fullname . "&lt;br/&gt;"; }之类的事情。我并不是说这会按照书面规定 100% 有效,但它至少应该让你朝着正确的方向前进。
  • 在尝试使用框架之前学习 php 语言的基础知识可能是个好主意?
  • @user2057530 让我们从这个开始……你知道stringarray 之间的区别吗?
  • 如果您只想测试这些值,请使用debug()

标签: php arrays cakephp


【解决方案1】:

test.ctp:

<?php echo "<pre>";print_r($users_list);echo "</pre>";  ?>

由于 find 的结果将是一个数组,所以上面的行将打印 $user_list 数组的所有元素,现在你必须根据你的逻辑使用这个数组,例如你可以使用 foreach() 来获取单个元素这个数组。

希望以上解释足以让你理解!

【讨论】:

    【解决方案2】:

    你的函数应该是这样的:

    public function test() {
        $this->set('users_list', $this->User->find('all', array('fields' => array('User.id',         'User.fullname'))));
    }
    

    然后在您看来,您应该执行一个 foreach 循环来输出如下值:

    foreach($users_list as $ul){
        echo 'This is the id: '.$ul[0]['User']['id'];
        echo ' This is the full name: '.$ul[0]['User']['fullname'];
    }
    

    我会这样做,但我不知道它是否是最好的

    如果你想要更简单的东西,你可以打印一个数组:

    print_r($users_list);
    

    【讨论】:

      【解决方案3】:

      您可以使用 String 类。它有一个很酷的方法,叫做 toList(),它用来打印一个数组,在最后一个元素之前有一个“and”。

      App::uses('String', 'Utility');
      echo String::toList($users_list);
      

      查看文档:http://book.cakephp.org/2.0/en/core-utility-libraries/string.html

      【讨论】:

        【解决方案4】:

        这对我有用。希望它也对你有用 这是我的ctp文件代码

         $this->Form->input('answertype',array('div'=>false,'label'=>false,'type'=>'select','multiple'> => 'checkbox', 'options' =>  $arrayOptions, 'class' => 'form-control  dropdown-menu-item '));
        

        当我在控制器中调试时,我得到了这样的结果

        array(
        'PackageDescription' => array(
            'name' => 'Special Di scout Package',
            'question' => '20',
            'revision' => '32',
            'experttype' => '100',
            'responsetime' => '6',
            'answertype' => array(
                (int) 0 => 'image',
                (int) 1 => 'audio'
            ),
            'support' => 'email&sms',
            'status' => 'public'
        )
        

        )

        我在我的模型中进行了一些更改以将任何数组存储为字符串

         public function beforeSave($options = array()) {
                if(!empty($this->data[$this->alias]['answertype']) && $this->data[$this->alias]['answertype'] != ''){
                    $subArr = $this->data[$this->alias]['answertype'];
                    $this->data[$this->alias]['answertype'] = implode(',', $this->data[$this->alias]['answertype']);
                    $my_model = ClassRegistry::init('Answer');
                    $AnswerInfo = $my_model->find('all', array('conditions' => array('Answer.name' => $subArr), 'fields' => array('Answer.name', 'Answer.name')));
                    if(!empty($AnswerInfo)){
                        $answertype = array();
                        foreach($AnswerInfo as $info){
                            $answertype[] = $info['Answer']['name'];
                        }
                        $this->data[$this->alias]['answertype'] = implode(',', $answertype);
                    }
                }   
                if (!empty($this->data[$this->alias]['name']) && empty($this->data[$this->alias]['slug'])) {
                    if(!isset($this->data[$this->alias]['id'])){
                        $this->data[$this->alias]['slug']=$this->stringToSlug($this->data[$this->alias]['name']);
                    }else{
                        $this->data[$this->alias]['slug']=$this->stringToSlug($this->data[$this->alias]['name'],$this->data[$this->alias]['id']);
                    }    
                }
                return true;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-27
          • 2015-02-21
          相关资源
          最近更新 更多