从性能角度来看,哪种代码更好?

问题描述 投票:0回答:1

我有一个动态表单,其中数据被添加到数据库中。 我很困惑哪种方法更好从循环外的会话中获取一些数据 或者直接我应该从循环内的会话中获取数据。 这是我的代码 1-从会话中获取数据并将其放入两个变量中,然后在循环内调用这些变量。

$user_name                  = $this->input->post('user_name');
 $organization_name          = $this->input->post('organization_name');
 $location                   = $this->input->post('location');
 $duration_time              = $this->input->post('duration_time');
 $yearly_expences            = $this->input->post('yearly_expences');
 $expences_source            = $this->input->post('expences_source');
 $remark                     = $this->input->post('remark');

 //-----
 $u_rec_id = $this->session->userdata('rec_id');
 $serial   = $this->session->userdata('serial');
 //-----
 $all_array = array();

 if( $user_name ) {
    if( is_array( $user_name ) ) {

        for( $j = 0 ; $j < count( $user_name ) ; $j++ ) {
           $this ->ci_form = array(
              'user_name'         => $user_name[$j],
              'organization_name' => $organization_name[$j],
              'location'          => $location[$j],
              'duration_time'     => $duration_time[$j],
              'yearly_expences'   => $yearly_expences[$j],
              'expences_source'   => $expences_source[$j],
              'remark'            => $remark[$j],
              'userid'            => $this->m_auth->get_user_id(),
              'u_rec_id'          => $u_rec_id,
              'serial'            => $serial,
              'regdate'           => date('Y-m-d H:m:s')

          );
          array_push( $all_array, $this->ci_form );
      }
   }

}

或者这样更好 直接在循环内从会话获取数据

$user_name                  = $this->input->post('user_name');
  $organization_name          = $this->input->post('organization_name');
  $location                   = $this->input->post('location');
  $duration_time              = $this->input->post('duration_time');
  $yearly_expences            = $this->input->post('yearly_expences');
  $expences_source            = $this->input->post('expences_source');
  $remark                     = $this->input->post('remark');


  $all_array = array();

  if( $user_name ) {
     if( is_array( $user_name ) ) {

        for( $j = 0 ; $j < count( $user_name ) ; $j++ ) {
            $this ->ci_form = array(
                'user_name'         => $user_name[$j],
                'organization_name' => $organization_name[$j],
                'location'          => $location[$j],
                'duration_time'     => $duration_time[$j],
                'yearly_expences'   => $yearly_expences[$j],
                'expences_source'   => $expences_source[$j],
                'remark'            => $remark[$j],
                'userid'            => $this->m_auth->get_user_id(),
                'u_rec_id'          => $this->session->userdata('rec_id'),
                'serial'            => $this->session->userdata('serial'),
                'regdate'           => date('Y-m-d H:m:s')
            );
            array_push( $all_array, $this->ci_form );
         }           
       }
    }
php codeigniter
1个回答
1
投票

使用第一种方法并节省一些函数调用:

$user_name                  = $this->input->post('user_name');
$organization_name          = $this->input->post('organization_name');
$location                   = $this->input->post('location');
$duration_time              = $this->input->post('duration_time');
$yearly_expences            = $this->input->post('yearly_expences');
$expences_source            = $this->input->post('expences_source');
$remark                     = $this->input->post('remark');

$u_rec_id = $this->session->userdata('rec_id');
$serial   = $this->session->userdata('serial');

$all_array = array();

foreach ((array)$user_name as $j=>$each) {
    $this ->ci_form = array(
        'user_name'         => $user_name[$j],
        'organization_name' => $organization_name[$j],
        'location'          => $location[$j],
        'duration_time'     => $duration_time[$j],
        'yearly_expences'   => $yearly_expences[$j],
        'expences_source'   => $expences_source[$j],
        'remark'            => $remark[$j],
        'userid'            => $this->m_auth->get_user_id(),
        'u_rec_id'          => $u_rec_id,
        'serial'            => $serial,
        'regdate'           => date('Y-m-d H:m:s')
    );

    array_push($all_array, $this->ci_form);
}
© www.soinside.com 2019 - 2024. All rights reserved.