开发者

Create a Dynamic PHP Array

this might be a bit tricky. First here is my function:

public function transfer_smf_userinfo($username)
{
    $sys_columns = implode(', ', $this->_config['bbauth']['columns']['sys']);
    $smf_co开发者_Python百科lumns = implode(', ', $this->_config['bbauth']['columns']['smf']);

    $question_marks;

    for($i = 0; $i > sizeof($this->_config['bbauth']['columns']['sys']); $i++)
    {
        if(sizeof($this->_config['bbauth']['columns']['sys']) == $i - 1)
        {
            $question_marks .'?';  
        }
        else {
            $question_marks .'?, ';
        }
    }

    $user = $this->smf_db->query('SELECT '. $smf_columns .' FROM `smf_members` WHERE member_name = ? LIMIT 1', array($username));

    if($user->num_rows > 0)
    {
        $user = $user->row_array();

        $user['register_date'] = date('Y-m-d H:i:s', $user['register_date']);
        $user['last_login'] = date('Y-m-d H:i:s', $user['last_login']);

        $transfer_user = $this->sys_db->query('INSERT INTO `members` ('. $sys_columns .') VALUES ('. $question_marks .')', array());

        if($transfer_user)
        {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

$sys_columns outputs:

id, username, password, email, url, avatar, b_date, r_date, l_date

$smf_columns outputs:

id_member, member_name, passwd, email_address, website_url, avatar, birthdate, date_registered, last_login

I need the array at the end of the line on the $transfer_user query to have a array like so.

array($user['column'], $user['another']);

But I need to do it dynamically with each row from $smf_columns.


If I understand the issue correctly. Try:

$data=array();
foreach($this->_config['bbauth']['columns']['smf'] as $column)
     $data[]=$user[$column];
$transfer_user = $this->sys_db->query(
     'INSERT INTO `members` ('. $sys_columns .') VALUES ('. $question_marks .')',
     $data);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜