如何在数组或Clist中添加新元素

时间:2014-06-17 08:55:38

标签: php arrays yii model

我有以下代码,

<?php

/**
 * This is the model class for table "questions".
 *
 * The followings are the available columns in table 'questions':
 * @property integer $queid
 * @property string $question
 *
 * The followings are the available model relations:
 * @property User[] $users
 */
class Questions extends CActiveRecord {

  public $que1 = "";
  public $wh1;
  public $que2 = "";
  public $wh2;
  public $que3 = "";
  public $wh3;
  public $que4 = "";
  public $wh4;
  public $que5 = "";
  public $wh5;
  public $que6 = "";
  public $wh6;
  public $list1 = array('abc', 'fghf', 'hjj');
  public $list2 = array('abc', 'fghf', 'hjj');

  /**
   * @return string the associated database table name
   */
  public function tableName() {
    return 'questions';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules() {

    // @todo Please remove those attributes that should not be searched.
    array('queid, question, que1,que2,que3,que4,que5,que6, wh1,wh2,wh3,wh4,wh5,wh6', 'safe', 'on' => 'search'),
    );
  }

  /**
   * @return array relational rules.
   */
  public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'users' => array(self::HAS_ONE, 'User', 'id'),
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels() {
    return array(
        'queid' => 'Queid',
        'question' => 'Question',
        'que1' => 'Q.1 Which events or actions are expected to be performed ?',
        'que2' => 'Q.2  Which input values or objects to be tested ?',
        'que3' => 'Q.3  Where this action,operation or functionality to be tested ?',
        'que4' => 'Q.4 Who all living or non-living actors/characters performing event/action/operation ?',
        'que5' => 'Q.5  When event/action/operation to be performed ?',
        'que6' => 'Q.6  What are the end results expected from this test?',
    );
  }

  /**
   * Returns the static model of the specified AR class.
   * Please note that you should have this exact method in all your CActiveRecord descendants!
   * @param string $className active record class name.
   * @return Questions the static model class
   */
  public static function model($className = __CLASS__) {
    return parent::model($className);
  }

  public function getlist() {


    if ($this->que1 === "") {

    } else {

      array_push($list1, $this->que1);
      return $list1;
    }
    if ($this->que2 === "") {

    } else {
//               array_push($list2, $this->que2);
//                    
//                return $list2; 
    }
  }

}

我使用array_push向数组中添加新元素,但它不起作用,新元素被覆盖,而且我总是得到一个值,所以我尝试使用默认值的数组,但结果相同。我也尝试使用Clist和add()函数,但这也无法正常工作。我做错了什么?

2 个答案:

答案 0 :(得分:0)

我不确定这是你在寻找什么,无论如何我在这里发布我的解决方案:

$array = array("el1", "el2");
$array[] = "el3";
print_r($array);

Online DEMO

答案 1 :(得分:0)

list1是一个类属性,因此$this对于访问它很重要,否则它将被视为getlist()的局部变量。

改变 -

array_push($list1, $this->que1);
return $list1;

array_push($this->list1, $this->que1);  
return $this->list1;