【问题标题】:save data in separate array in each iteration在每次迭代中将数据保存在单独的数组中
【发布时间】:2013-06-19 06:28:27
【问题描述】:
$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);

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

对不起,我是数组中的一周.. 请告诉我如何打印数组...如果我想在我的视图页面中显示第一次迭代结果

【问题讨论】:

  • 使用'$this->{Model}->create()'
  • 您是要将每次迭代的数据保存到同一级别的$recent数组中还是每次迭代都应保存在$recent数组中的单独数组中?
  • 我想将最近的每次迭代保存在单独的数组中......所以如果我想在我的视图页面中显示第一次迭代的结果,我可以显示它
  • 将 $recent 变为 $recent[]。这将有循环的所有记录。稍后您可以在视图中迭代此循环

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


【解决方案1】:

如果您只想向数组中添加新元素而不删除其内容,您可以这样做:

$recent[] = 'new_string1';

或者如果你有更多元素:

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

不过以后请看说明书:http://php.net/manual/en/function.array-push.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2022-01-05
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多