在每次迭代中将数据保存在单独的数组中

时间:2013-06-19 06:28:28

标签: arrays cakephp for-loop foreach cakephp-2.0

$this->layout='defaultall';

    $id = $this->Auth->user('idUser');
    $data= $this->Contact->find('all',array(
        'conditions' => array('Contact.User_id' => $id)));
          $this->set('contacts',$data);


 $this->loadModel('Calllog');

  /*  for($i=1; $i < 2; $i++)
    {
        $mobile =$data[$i]['Contact']["mobileNo"];
        $work =$data[$i]['Contact']["workNo"];
        $home =$data[$i]['Contact']["homeNo"];*/



   for($data as $datas){

      $mobile=  $datas['Contact']['mobileNo'];
      $home=  $datas['Contact']['homeNo'];
      $work=  $datas['Contact']['workNo'];




      $recent=$this->Calllog->find('all',
          array('order'=>'Calllog.idCallLog DESC',
              'limit' => 1,
              'conditions' => array('Calllog.mobileNo' => array($mobile,$work,$home ),
                  'Calllog.User_id' => $id


              )));

      //here array want to declare


      $this->set('a',$recent);

//这里我要声明另一个数组,它保存每次迭代的数据。因为我不想覆盖数据

抱歉,我在数组周...并且告诉我如何打印数组...如果我想在我的视图页面中显示第一次迭代结果

1 个答案:

答案 0 :(得分:1)

如果你只是想在不删除内容的情况下向数组添加新元素,你就可以这样做:

$recent[] = 'new_string1';

或者如果您有更多元素:

array_push($recent, 'new_string1', 'new_string2');

但将来请阅读手册:http://php.net/manual/en/function.array-push.php